I have tested this code with many version of GCC, three under Linux x86 PC and one into x86 Mac OS X. This bug appears with version 3.4.5 (Gentoo 3.4.5, ssp-3.4.5-1.0, pie-8.7.9), in a Gentoo x86_64 Linux PC, and with Gcc 4.1.2 (Ubuntu 4.1.2-0ubuntu4) and 4.1.2 20061115 (prerelease) (Debian 4.1.1-21). It doesn't appear in x86 Mac Os X 10.4 with "version 4.0.1 (Apple Computer, Inc. build 5250)" This very simple code show the gcc's bug. #include <stdio.h> // for printf struct ELT { int item; } one_elt; int main() { struct ELT *two_elt; long *src,*dst; two_elt = malloc(sizeof(struct ELT)); src = (long *)&one_elt; dst = (long *)two_elt; dst[0] = src[0]; two_elt->item = 27; malloc(2); printf("27 = %d\n",two_elt->item); return( 0); } When you compile with -O1 or -O0, we get "27=27", but when we compile with -O2 or -O3 switch, we get "27=0" Gcc "forget" code's dependancy.
Created attachment 14215 [details] Preprocessed file that show the bug
> It doesn't appear in x86 Mac Os X 10.4 with "version 4.0.1 (Apple Computer, > Inc. build 5250)" Because the GCC released by Apple defaults to -fno-strict-aliasing. > Gcc "forget" code's dependancy. No, your code violates the aliasing rules of the ISO C standard and GCC is allowed to optimize it. Either fix your code or use -fno-strict-aliasing.