void test(int *&p) { p+= 1; // modifies p in register (char*&)p+= 8; // modifies p in memory // finally the value from register wins }
This prints 4 (BAD): g++ -O2 -march=athlon bug.cc -o bug; ./bug; echo $? This prints 12 (CORRECT): g++ -O1 -march=athlon bug.cc -o bug; ./bug; echo $? This prints 12, but works by chance (assembly code is incorrect): g++ -O2 bug.cc -o bug; ./bug; echo $? # gcc -v Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386-redhat-linux Thread model: posix gcc version 3.4.2 20041006 (Red Hat 3.4.2-5) -- Summary: Pointer reference optimized into register is not used when referenced through type cast. Product: gcc Version: 3.4.2 Status: UNCONFIRMED Severity: critical Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: zybi at talex dot pl CC: gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17998