I would like to know if there is anything that prevents doing this in ANSI C (or anything prior to C99 which has the strict aliasing rule).
const int n = 1000;
double *a = (double *) malloc(n * sizeof(double));
// Weird aliasing
a = (double *) (&a);
f(a, n);
free(a);
this questions comes from the fact that the Intel compiler does vectorize the following code
void f(double *a, int n) {
int k;
for (k = 0; k < n; ++k) {
a[k] = a[k] + 1.0;
}
}
without the -ansi-aliasing option (by default, Intel compilers don't use the strict aliasing rule). My guess is that it should not as the previous code changes what a points to at the first loop.
Francois
explanation : As an explanation is often asked on the reason to this, you can read one of Chris Lattner post on http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html in the "Violating Type Rules" section. It seemed to me that he was using the strict aliasing rule and therefore C99 to state that what he does is not valid.