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
  • I don't know what's happening about the bug you're getting, but I would like to point out that a (volatile int) cast does nothing more that just (int) does on its own. volatile applies to accessing a location, not a value. If you declared a function as pure using a pragma the compiler would be entitled to only call it once for the same parameters even if you put (volatile int) before each call.

    The only really useful use of volatile in a cast is in things like

    *(volatile int *)intptr

Reply
  • I don't know what's happening about the bug you're getting, but I would like to point out that a (volatile int) cast does nothing more that just (int) does on its own. volatile applies to accessing a location, not a value. If you declared a function as pure using a pragma the compiler would be entitled to only call it once for the same parameters even if you put (volatile int) before each call.

    The only really useful use of volatile in a cast is in things like

    *(volatile int *)intptr

Children
More questions in this forum