http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49915
Summary: Function call with 2-D arrays and -O2 (or strict-aliasing and inlining) gives random results Product: gcc Version: 4.1.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c AssignedTo: unassig...@gcc.gnu.org ReportedBy: jquin...@entrenet.com Created attachment 24872 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24872 test.i from gcc -v -save-temps -O2 test.c A function call with two-dimensional arrays and -O2 (or the switches -fstrict-aliasing and -finline) gives random results. Complete source code (14 lines): static void copy(int n, int a[][n], int b[][n]) { int i, j; for (i = 0; i < 2; i++) // 'n' not used in this example for (j = 0; j < 2; j++) // 'n' hard-coded to 2 for simplicity b[i][j] = a[i][j]; } int main(int argc, char *argv[]) { int a[2][2] = {{1, 2},{3, 4}}; int b[2][2]; copy(2, a, b); printf("%d %d %d %d\n", b[0][0], b[0][1], b[1][0], b[1][1]); return 0; } Test results: gcc -O1 test.c && a.out 1 2 3 4 gcc -O2 test.c && a.out 12173125 -1079787588 -1079787736 13389812 gcc -O1 -fstrict-aliasing -finline test.c && a.out 12173125 -1080040260 -1080040408 13389812 All three tests also produced the (obvious) warning: test.c: In function 'main': test.c:12: warning: incompatible implicit declaration of built-in function 'printf' gcc -v -save-temps -O2 test.c Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-libgcj-multifile --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --disable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux Thread model: posix gcc version 4.1.2 20080704 (Red Hat 4.1.2-50) /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -E -quiet -v test.c -mtune=generic -O2 -fpch-preprocess -o test.i ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include" #include "..." search starts here: #include <...> search starts here: /usr/local/include /usr/lib/gcc/i386-redhat-linux/4.1.2/include /usr/include End of search list. /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -fpreprocessed test.i -quiet -dumpbase test.c -mtune=generic -auxbase test -O2 -version -o test.s GNU C version 4.1.2 20080704 (Red Hat 4.1.2-50) (i386-redhat-linux) compiled by GNU C version 4.1.2 20080704 (Red Hat 4.1.2-50). GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: d8d95095eb3c93dae4bed2137d559f95 test.c: In function 'main': test.c:12: warning: incompatible implicit declaration of built-in function 'printf' as -V -Qy -o test.o test.s GNU assembler version 2.17.50.0.6-14.el5 (i386-redhat-linux) using BFD version 2.17.50.0.6-14.el5 20061020 /usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. test.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o