volatile int typecast return value. Could this make problem?

Hello experts, I'm a beginner of embedded system development.

I have question.about difference of two code.

at first, suppose there is function like

   int SomeFunction(void) {

      ......

       return (some integer);

}

And using this function return value like these

  • int retval;

       retval = SomeFunction();

       if (retval == {some Variable})  {

         . . .

       }

  • if ( {some Variable} == (volatile int) SomeFunction()) {

          ....

        }

    

former one doesn't make any problem, but when i used like latter one.. some times, retval is not correct and some strange value remains on r0 register...;

I don't know what is problem on latter one exactly...;

Could explain what is the problem of these?

Thank you very much for reading this..... though it is written by pool english...

Parents
  • Thanks a lot yasuhikokoumoto.

    Your answer is so helpful.

    But i stiil  have a question.

    Strangely, when I used the return value by saving volatile variable, there is no problem.

    In other word.. if I used like..

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    int SomeFunction(void)

    {

        ...

       

        return {Some integer but changeable like Tick..}

    }

    int main (void)

    {

        (volatile int) retval;

        int comparedVariable = {Some value} ;

        ...

       

        retval = SomeFunction(void);

       

        // There is no Bugs.

        if (retval  == compareVriable) {

            foo();

        } else {

            exit(1);

        }

    }

    Colored by Color Scripter

    cs

    above way doesn't make any problem....

    but if I compare using typecasting like..

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    int SomeFunction(void)

    {

        ...

       

        return {Some integer but changeable like Tick..}

    }

    int main (void)

    {

        int comparedVariable = {Some value} ;

        ...

       

        retval = SomeFunction(void);

       

        // Some times retrun value saved in r0 is wrong.

        if (compareVriable == (volatile int) SomeFunction()) {

            foo();

        } else {

            exit(1);

        }

    }

    Colored by Color Scripter

    cs

    makes the problem.

    when i compared the assembly code..... also, there is no big difference.. Just r0 value is wrong, when comparing.

    Could I think it is compiler bug as you said?

    Thx a lot.

Reply
  • Thanks a lot yasuhikokoumoto.

    Your answer is so helpful.

    But i stiil  have a question.

    Strangely, when I used the return value by saving volatile variable, there is no problem.

    In other word.. if I used like..

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    int SomeFunction(void)

    {

        ...

       

        return {Some integer but changeable like Tick..}

    }

    int main (void)

    {

        (volatile int) retval;

        int comparedVariable = {Some value} ;

        ...

       

        retval = SomeFunction(void);

       

        // There is no Bugs.

        if (retval  == compareVriable) {

            foo();

        } else {

            exit(1);

        }

    }

    Colored by Color Scripter

    cs

    above way doesn't make any problem....

    but if I compare using typecasting like..

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    int SomeFunction(void)

    {

        ...

       

        return {Some integer but changeable like Tick..}

    }

    int main (void)

    {

        int comparedVariable = {Some value} ;

        ...

       

        retval = SomeFunction(void);

       

        // Some times retrun value saved in r0 is wrong.

        if (compareVriable == (volatile int) SomeFunction()) {

            foo();

        } else {

            exit(1);

        }

    }

    Colored by Color Scripter

    cs

    makes the problem.

    when i compared the assembly code..... also, there is no big difference.. Just r0 value is wrong, when comparing.

    Could I think it is compiler bug as you said?

    Thx a lot.

Children