int weird(float *fp){ // access an int as an int (see caller), // so not an aliasing violation return *(int*)fp; } int main(int argc, char *argv[]){ return weird((float*)&argc); }
I just tried this code with gcc 4.4.5 on 32-bit powerpc using -O2 -W -Wall. Assembly code for the weird function looks OK, both inlined and not, but that certainly isn't proof that gcc will always tolerate such code. I recall that there were problems handling this type of code. (never mind any non-conformant callers that actually pass a pointer to a float -- not that gcc would be able to see them in separately compiled files) So, is it fixed now? (what gcc version?) If not, is it at least fixed if I change "float" to "void" and/or "unsigned char"? BTW, oddly it looks like gcc tolerates a genuine aliasing violation as well now. (passing the value as a float) Of course, that may just be my luck with the optimizer.