GCC has the -fstrict-aliasing option, which allows you to optimize aliases globally and expects you to get nothing in an illegal way. This optimization is enabled for -O2 and -O3 , I believe.
C ++ has well-defined alias rules, although standardized code will not conflict with a strict alias. In particular, this means that you are not allowed to access one variable with a pointer to another type:
float f; int * p = reinterpret_cast<int*>(&f);
An important exception to this rule is that you can always access any variable with a pointer to char . (This is necessary for serialization through I / O.)
Alias โโrules do not help the compiler to find out if any pointers of the same aliases are to each other. Consider this:
void add(float * a, float * b, float * c) { *c = *a + *b; }
Here the compiler cannot know if c points to a different memory than a or b , and this should be careful. I think restrict here matters, essentially promising that float * restrict c means no one is an alias of c .
Kerrek SB
source share