An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. It is possible to reference a volatile object by using a non-volatile value, but the resulting behavior is undefined. According to C99 Section 6.7.3, "Type qualifiers," Paragraph 5:
If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.
This also applies to objects that behave as if they were defined with qualified types, such as an object at a memory-mapped input/output address.
...
In this example, a volatile object is accessed through a non-volatile-qualified reference, resulting in undefined behavior.
...
The assignment ipp = &ip
is unsafe because it would allow the following valid code that follows to reference the value of the volatile object i
through the non-volatile qualified reference ip
. In this example, the compiler may optimize out the entire if block because it is not possible that i != 0
if i
is not volatile.
...