09-10-2015, 11:41 AM
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.whereas this is not the case in the original code
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))
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