Consider the following snippet as an example:
*pInt = 0xFFFF;
*pFloat = 5.0;
Since they are int
and float
pointers, the compiler will assume they don't alias and can exchange them for example.
Now let's assume we spice it up with this:
*pInt = 0xFFFF;
*pChar = 'X';
*pFloat = 5.0;
Since char*
is allowed to alias anything, it may point to *pInt
, so the assignment to *pInt
cannot be moved beyond the assignment of *pChar
, because it may legitimately point to *pInt
and set its first byte to 'X'.
Similarly pChar
may point to *pFloat
, assignment to *pFloat
cannot be moved before the char assignment, because the code may intend to nullify the effects of the previous byte setting by reassigning the *pFloat
.
Does this mean I can write and read through char*
to create barriers for rearrangement and other strict aliasing related optimizations?
*a = 1
has been moved down and merged with*a = *a + 1
. In the second case, this is prevented. – Encratia