Having recently read that the main reason why fortran is faster than c/c++ in numerical computations is because there is no pointer aliasing.
Apparently, using
GCC has the option -fstrict-aliasing
which enables aliasing optimizations globally and expects you to ensure that nothing gets illegally aliased. This optimization is enabled for -O2
and -O3
I believe.
C++ has well defined aliasing rules, though, and standard-compliant code will not conflict with strict aliasing. In particular this means you're not allowed to access one variable through a pointer to a different type:
float f;
int * p = reinterpret_cast(&f); // uh-oh
*p = 0x3FF00000; // breaks strict aliasing
The crucial exception to this rule is that you can always access any variable through a pointer to char
. (This is necessary for serializing through IO operations.)
The aliasing rules do not help the compiler to know whether any pointers of the same type alias each other. Consider this:
void add(float * a, float * b, float * c) { *c = *a + *b; }
Here the compiler cannot know whether c
points to different memory than a
or b
and has to be careful. I think this is where restrict
makes a difference, essentially by promising that float * restrict c
means that nobody aliases c
.