http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51885
Bug #: 51885 Summary: g++ compiler options -O2 and -O3 modifies program results Classification: Unclassified Product: gcc Version: 4.6.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: laurentlouis.mau...@wanadoo.fr Created attachment 26356 --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=26356 example program with comments platform : Fedora 16 x86_64 compiler : gcc-c++-4.6.2-1.fc16.x86_64 see attaches source file ; it is an example of the quicksort algorithm written with pointers and templates one template is specialised and uses a reinterpret_cast (it is part of a bigger file) it sorts this array : {12, -1, 2} a) first test (compilation without any optimisation) compilation : g++ main.cpp && ./a.out result OK : {-1, 2, 12} b) second test (-O2 optimisation) compilation : g++ -O2 main.cpp && ./a.out wrong result : {2, 12, -1} 3) third test (-O3 optimisation) compilation : g++ -O3 main.cpp && ./a.out wrong result : {2, 12, -1} Strange behaviour : if i include between line #65 and line #67 (see attached file) std::cout<<*ptr_start<<std::endl; then, the results are good in any case i believe this is a bug ... the optimisation option should not change the program result best regards Laurent