Strict pointer aliasing: is access through a 'volatile' pointer/reference a solution?
Posted
by doublep
on Stack Overflow
See other posts from Stack Overflow
or by doublep
Published on 2010-06-05T19:50:14Z
Indexed on
2010/06/05
19:52 UTC
Read the original article
Hit count: 229
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.
© Stack Overflow or respective owner