Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
12.4, reading volatile side effect
#6
The && operator does not evaluate its right-hand operand if the left-hand operand evaluates to False. Therefore any side-effect in the right-hand expression will not occur if the left-hand expression evaluates to False.

Re-writing the code ensures that the volatile is always read.
Code:
#define REGISTER            (*((uword volatile *) 0xEA2CU))
uword reg;
reg = REGISTER;             /* volatile read will always happen */
if(xxx && (reg > 0))
whereas this is not the case in the original code
Code:
#define REGISTER            (*((uword volatile *) 0xEA2CU))
if (xxx && REGISTER)        /* volatile read will only occur if xxx evaluates to True */
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 5 Guest(s)