This code gives "0 0" at first call to printf (wrong) and "4 4" at second call (good). Works correctly with -O1, errors occur with -Os or -O2 or above. void read8(int *x) { x[0] = 4; x[1] = 4;} int main() { static double equiv[1]; double r; read8(&r); *(equiv) = r; printf("%d %d\n",((int *)equiv)[0],((int *)equiv)[1]); printf("%d %d\n",((int *)equiv)[0],((int *)equiv)[1]); return 0; }
Wrong code indeed, but on your side: your C code violates the aliasing rules of the ISO C language. Either fix it or pass -fno-strict-aliasing to the compiler. See the entry of -fstrict-aliasing in the manual for more detailed info.
Subject: Re: Wrong code in gcc-3.3.5 and gcc-3.4.2 (4.0 not checked) Thanks a lot! This error occured originally in the f2c-translated FORTRAN code. It works flawlessly with -fno-strict-aliasing. Krzysztof PS who says, there is no support with free software ???
Reopning to ...
Mark as a dup of bug 21920. *** This bug has been marked as a duplicate of 21920 ***