Consider the following minimal example :
--cut after--
#include<iostream>
struct A {
long double * t;
int n;
A() { n=2; t=new long double[2]; }
long double &operator()(unsigned i) {return t[i];};
void bug();
};
void A::bug()
{
A a;
for(int k=0;k<n;k++) {
if(k!=0)
a(k-1)=0;
a(k)=1;
}
if(a(0)==1)
std::cerr << "bug" << std::endl;
}
int main()
{
A a;
a.bug();
return 0;
}
--cut before--
When compiled with -O with g++ 4.0.1 or 4.0.3, "bug" is displayed, which is
incorrect. This does not happen when no optimization is performed.
Reproducible with the two following compilers (output of "g++ -v"):
Using built-in specs.
Reading specs from /usr/local/util/etc/gcc-3.4-fedora.specs
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--host=i386-redhat-linux
Thread model: posix
gcc version 4.0.1 20050727 (Red Hat 4.0.1-5)
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v
--enable-languages=c,c++,java,f95,objc,ada,treelang --prefix=/usr
--enable-shared --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix --enable-nls
--program-suffix=-4.0 --enable-__cxa_atexit --enable-clocale=gnu
--enable-libstdcxx-debug --enable-java-awt=gtk-default --enable-gtk-cairo
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.0-1.4.2.0/jre --enable-mpfr
--disable-werror --with-tune=pentium4 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.0.3 (Ubuntu 4.0.3-1ubuntu5)
Not reproducible with g++ 3.5 on i486-linux.
This bug also disappears when long double is replaced with double.
--
Summary: Incorrect optimization inside member function
Product: gcc
Version: 4.0.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: pierre at senellart dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28707