The attached file osbugs.cpp will print a "BUG!!!!" message when compiled with the -Os switch with g++ 3.4.4 on powerpc. This is due to the optimizer generating incorrect code, by apparently incorrectly determining that S::p is not being changed by the method S::init() before S::~S() is called.
I found this bug originally in the 3.3.5 version of the compiler, and it was causing a strange crash down in the basic_string code, but I've been able strip the code down to what I think is about the minimum needed to reproduce the optimization failure. No pre-processor directives are in the attached source file. The bug has been reproduced in mips-linux 3.3.5, and i386-linux 3.3.2 and 3.3.5. The bug does not appear in i386-linux 3.4.4. Sample output, showing that this bug occurs across several different distributions of GCC: powerpc, 3.4.4: $ g++-3.4 --version | head -n1 g++-3.4 (GCC) 3.4.4 20050314 (prerelease) (Debian 3.4.3-13) $ g++-3.4 -O2 osbugs.cpp -o osbugs $ ./osbugs p is 0x10011050 and p2 is 0x10011050 (should be non-zero and equal) $ g++-3.4 -Os osbugs.cpp -o osbugs $ ./osbugs p is 0x0 and p2 is 0x0 (should be non-zero and equal) BUG!!!! p is 0x10011050 and p2 is 0x0 i386, 3.3.5: $ g++ --version | head -n1 g++ (GCC) 3.3.5 (Debian 1:3.3.5-13) $ g++ -O2 osbugs.cpp -o osbugs $ ./osbugs p is 0x804a008 and p2 is 0x804a008 (should be non-zero and equal) $ g++ -Os osbugs.cpp -o osbugs $ ./osbugs p is 0x0 and p2 is 0x0 (should be non-zero and equal) BUG!!!! p is 0x804a008 and p2 is 0x0 i386, 3.3.2: $ g++ --version | head -n1 g++ (GCC) 3.3.2 (Red Hat Linux 3.3.2-1) $ g++ -O2 osbugs.cpp -o osbugs $ ./osbugs p is 0x8f0f008 and p2 is 0x8f0f008 (should be non-zero and equal) $ g++ -Os osbugs.cpp -o osbugs $ ./osbugs p is 0x0 and p2 is 0x0 (should be non-zero and equal) BUG!!!! p is 0x8497008 and p2 is 0x0 mips, 3.3.5: $ mips-linux-g++ --version | head -n1 mips-linux-g++ (GCC) 3.3.5 $ mips-linux-g++ -O2 osbugs.cpp -o osbugs [mipsbox]$ ./osbugs p is 0x10000240 and p2 is 0x10000240 (should be non-zero and equal) $ mips-linux-g++ -Os osbugs.cpp -o osbugs [mipsbox]$ ./osbugs p is 0x10000240 and p2 is 0x0 (should be non-zero and equal) BUG!!!! p is 0x10000240 and p2 is 0x0 -- Summary: Strange optimization bug when using -Os Product: gcc Version: 3.4.4 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: dahowell at directv dot com CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: powerpc-linux GCC host triplet: powerpc-linux GCC target triplet: powerpc-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22167