If I have a structure such as this:
typedef struct a_Struct { int x; int y; } aStruct;
Then pass a variable of type pointer to aStruct into a function such as:
void aFunction( aStruct* param ) { param->x = 3; if( param->x == 3 ) { param->y ++; } }
What is the optimizer permitted to do?
Param->x is set to 3 then immediately tested to see if it is three. As param is not volatile it might be reasonable for an optimizer to miss this set and test out as it could always expect the if() to evaluate to true. If it does this then the function might then just increment the y member and leave the x member unchanged.
But param is passed in by reference so the optimizer has not way of knowing how the x member is used outside of the function, so when the function exits I think it should be expected that x should be set to 3. Is this assumption safe?
If when I call aFunction I cast the parameter to a pointer to volatile I get a compiler warning that the volatile has no meaning on the cast (which is correct).
I could define the function as void aFunction( volatile aStruct* param ) I think this would guarantee that x and y were set.
I would appreciate your comments.