An object that has volatile-qualified type may be modified in ways unknown to the implementation or have other unknown side effects. Referencing a volatile object by using a non-volatile lvalue results in is undefined behavior. The C Standard, 6.7.3 [ISO/IEC 9899:2011], states:
...
In this noncompliant code example, a volatile object is accessed through a non-volatile-qualified reference, resulting in undefined behavior:
...
The assignment ipp = &ip
is not safe because it allows the 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 *ip != 0
must be false if the object to which ip
points is not volatile.
...
Accessing an object with a volatile-qualified type through a reference with a non-volatile-qualified type is undefined behavior.
...