Search code examples
c++volatilestrict-aliasingtype-punning

Strict pointer aliasing: is access through a 'volatile' pointer/reference a solution?


On the heels of a specific problem, a self-answer and comments to it, I'd like to understand if it is a proper solution, workaround/hack or just plain wrong.

Specifically, I rewrote code:

T x = ...;
if (*reinterpret_cast <int*> (&x) == 0)
  ...

As:

T x = ...;
if (*reinterpret_cast <volatile int*> (&x) == 0)
  ...

with a volatile qualifier to the pointer.

Let's just assume that treating T as int in my situation makes sense. Does this accessing through a volatile reference solve pointer aliasing problem?

For a reference, from specification:

[ Note: volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation. See 1.9 for detailed semantics. In general, the semantics of volatile are intended to be the same in C++ as they are in C. — end note ]

EDIT:

The above code did solve my problem at least on GCC 4.5.


Solution

  • Volatile can't help you avoid undefined behaviour here. So, if it works for you with GCC it's luck.

    Let's assume T is a POD. Then, the proper way to do this is

    T x = …;
    int i;
    memcpy(&i,&x,sizeof i);
    if (i==0)
      …
    

    There! No strict aliasing problem and no memory alignment problem. GCC even handles memcpy as an intrinsic function (no function call is inserted in this case).