Implementation Dependency - Qualifiers (F.3.10)

For access to an object that has a type qualified as volatile, all access is retained. (3.5.5.3) The volatile attribute maintains consistency in memory access to data objects. Volatile objects are read from memory each time their value is needed, and written back to memory each time they are changed. When optimizing the code, the compiler cannot remove any access to a volatile variable. The volatile attribute is useful for objects (such as the system clock) that have values that may be changed in ways unknown to your program.

For example, in the following program fragment,

volatile int i;
i=3;
i=5;
i=6;

all of these statements are kept by the compiler even if the O option to optimize the code is active. If int i was not defined as volatile, only i=6 would be kept in the optimized code.



Implementation-Defined Behavior
Implementation Dependencies
O, optimize Compiler Option