gcc -O2 produces bad code for function given below. Correct code is generated for no optimization or -O. The problem is that with -O2 the 3rd param passed to fprintf is uninitialized garbage, not the bytes set by the assignment to T0. More presisely, the bad code generated is: main: pushl %ebp movl %esp, %ebp subl $12, %esp movl stderr, %eax movl -4(%ebp), %edx movl $0x3fa00000, -4(%ebp) pushl %edx pushl $.LC0 pushl %eax call fprintf movl %ebp, %esp popl %ebp ret That is, %edx is set prior to the initialization of -4(%ebp). And %edx is then passed to fprintf, causing garbage to be printed. The uuencoded, gzipped, preprocessed file is attached. This error also occurs using CodeSourcery's Online Test Compiler. The code is slightly different than what I see using 3.0, but it is still wrong. Release: gcc version 3.0 Environment: i686-pc-linux-gnu Configured with: /export/gnu/gcc-3.0/configure --enable-languages=c++ Thread model: single linux 2.4.4 running on rh7.1 (Pentium III) How-To-Repeat: #include <stdio.h> main() { float T0; T0=1.25; fprintf(stderr, "%x\n", *(unsigned int*)&T0); }
State-Changed-From-To: open->closed State-Changed-Why: Not a bug; your code violates ANSI C aliasing rules.
Reopening to ...
Mark as a dup of bug 21920. *** This bug has been marked as a duplicate of 21920 ***