The attached code generates a segmentation fault when compiled and run with -O3. The error disappears if I inline any of the functions, remove any of the unused class members, change the argument to min to be const int instead of const int&, etc, comment out any of the lines which do nothing, etc. -fno-strict-aliasing also makes it go away. It's still a little long, but I can't seem to reduce it any further.
================ BEGIN COMPILE COMMAND ===================== ring:bug% /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/bin/g++ -v -save-temps -o bug -O3 -Wall bug.cpp Using built-in specs. Target: x86_64-unknown-linux-gnu Configured with: ../gcc-4.1.1/configure --prefix=/usr/local/compilers/gcc-4.1.1-x86_64-x86_64 --enable-languages=c,c++ Thread model: posix gcc version 4.1.1 /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/libexec/gcc/x86_64-unknown-linux-gnu/4.1.1/cc1plus -E -quiet -v -D_GNU_SOURCE bug.cpp -mtune=k8 -Wall -O3 -fpch-preprocess -o bug.ii ignoring nonexistent directory "/usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/../../../../x86_64-unknown-linux-gnu/include" #include "..." search starts here: #include <...> search starts here: /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/../../../../include/c++/4.1.1 /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/../../../../include/c++/4.1.1/x86_64-unknown-linux-gnu /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/../../../../include/c++/4.1.1/backward /usr/local/include /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/include /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/include /usr/include End of search list. /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/libexec/gcc/x86_64-unknown-linux-gnu/4.1.1/cc1plus -fpreprocessed bug.ii -quiet -dumpbase bug.cpp -mtune=k8 -auxbase bug -O3 -Wall -version -o bug.s GNU C++ version 4.1.1 (x86_64-unknown-linux-gnu) compiled by GNU C version 4.1.1. GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072 Compiler executable checksum: 9d1c719cee21891cbbb928e6cdd7db48 as -V -Qy -o bug.o bug.s GNU assembler version 2.17.50.0.3-6 (x86_64-redhat-linux) using BFD version 2.17.50.0.3-6 20060715 /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/libexec/gcc/x86_64-unknown-linux-gnu/4.1.1/collect2 --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o bug /usr/lib/../lib64/crt1.o /usr/lib/../lib64/crti.o /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/crtbegin.o -L/usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1 -L/usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 bug.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/local/compilers/gcc-4.1.1-x86_64-x86_64/lib/gcc/x86_64-unknown-linux-gnu/4.1.1/crtend.o /usr/lib/../lib64/crtn.o ring:bug% ./bug Segmentation fault ================ END COMPILE COMMAND ===================== ================ BEGIN bug.cpp ===================== struct VECTOR { int x,y,z; VECTOR() :x(0),y(0),z(0) {} }; struct ARRAY { int m; bool* p; ARRAY() { m=0; p=new bool; *p=false; } ~ARRAY(){} }; struct ARRAY_VECTOR { int m; VECTOR* p; ARRAY_VECTOR() { m=0; p=new VECTOR; *p=VECTOR(); } ~ARRAY_VECTOR(){} }; int blah; struct C{}; struct INDIRECT:public C { ARRAY& array; INDIRECT(ARRAY& array) :array(array) {} ~INDIRECT(){} }; bool get(const INDIRECT& array) { return *array.array.p; } int count(const C& self) { return get(static_cast<const INDIRECT&>(self)); } INDIRECT subset(ARRAY& array,const VECTOR* indices) { return array; } int& get(VECTOR& v) { return v.x; } void remove(const VECTOR& v) { VECTOR r;get(r)=v.x; } int min(const int& a,const int& b) { if(b < a) return b; return a; } void resize(ARRAY_VECTOR& array) { VECTOR* p_new=new VECTOR; const VECTOR initialization_value=VECTOR(); int unused=min(0,0);unused=0; *p_new=*array.p; array.m=0; array.p=0; array.p=p_new; } struct MESH { ARRAY* node_on_boundary; ARRAY a; }; struct STRAIN { MESH& mesh; STRAIN(MESH& mesh) :mesh(mesh) {} }; struct FVM { ARRAY_VECTOR boundary_to_element; ARRAY a,b,c; FVM(STRAIN& strain); }; FVM::FVM(STRAIN& strain) { VECTOR e; blah=count(subset(*strain.mesh.node_on_boundary,&e)); resize(boundary_to_element); VECTOR element; remove(element); } int main() { MESH mesh; STRAIN strain(mesh); ARRAY node_on_boundary; strain.mesh.node_on_boundary=&node_on_boundary; new FVM(strain); return 0; } ================ END bug.cpp ===================== -- Summary: incorrect code generation, possibly related to casting/aliasing Product: gcc Version: 4.1.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: irving at cs dot stanford dot edu GCC build triplet: x86_64-unknown-linux-gnu GCC host triplet: x86_64-unknown-linux-gnu GCC target triplet: x86_64-unknown-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30080