painful optimization bug in 19980921

Corey Kosak kosak@cs.cmu.edu
Sun Sep 27 12:51:00 GMT 1998


Hello,

This strange little program prints the wrong answers when optimization
is turned on.  Note that the if(0) line is required to trigger the bug.


% g++ -v
Reading specs from /afs/cs.cmu.edu/project/cmcl-kosak/various-gcc/egcs-19980921/i386_linux2/lib/gcc-lib/i686-pc-linux-gnulibc1/egcs-2.92.11/specs
gcc version egcs-2.92.11 19980921 (gcc2 ss-980609 experimental)

% g++ pp2.cc ; a.out
1000
1000

% g++ -O pp2.cc ; a.out
1000
666

% cat pp2.cc
#include <iostream.h>

template<class T> void func(const T &x) {}

int main()
{
  unsigned int optimalDistance=1000;
  unsigned int currentDistance=666;

  cerr << optimalDistance << "\n";
  for(int i=0; i<1; ++i) { 
    cerr << optimalDistance << "\n";
    
    if(currentDistance<optimalDistance) {
      optimalDistance=currentDistance;
      if(0)
        func(optimalDistance); //never called!
    }
  }
}



More information about the Gcc-bugs mailing list