http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53990
Bug #: 53990 Summary: wrong "optimisation": automatic variable doesn't removed at fuction exit Classification: Unclassified Product: gcc Version: 4.6.2 Status: UNCONFIRMED Severity: major Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: vol.li...@gmail.com I use C++ with the following specs by g++ -v Using built-in specs. Target: x86_64-manbo-linux-gnu Configured with: ../configure --prefix=/usr --libexecdir=/usr/lib --with-slibdir=/lib64 --with-bugurl=https://qa.mandriva.com/ --mandir=/usr/share/man --infodir=/usr/share/info --enable-checking=release --enable-languages=c,c++,ada,fortran,objc,obj-c++,java --build=x86_64-manbo-linux-gnu --host=x86_64-manbo-linux-gnu --with-cpu=generic --with-system-zlib --enable-threads=posix --enable-shared --enable-objc-gc --enable-long-long --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --enable-java-awt=gtk --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-gtk-cairo --disable-libjava-multilib --enable-ssp --disable-libssp --disable-werror --with-ppl --with-cloog --with-python-dir=/lib/python2.6/site-packages Thread model: posix gcc version 4.4.1 (GCC) I've also done tests with gcc version 4.4.5 (Debian 4.4.5-8) and gcc version 4.6.2 (SUSE Linux). See the next code struct A { int d; A operator*(const A&) const; A operator+(const A&) const; } a; A A::operator*(const A& a) const { A c; c.d = d*a.d; return c; } A A::operator+(const A& a) const { cout << *this; return a; } "return a;" causes the call of the copy constructor, but "return c;" doesn't call this constructor---it returns (does not destroy!) automatic (!) variable c. Is this optimization in C++ standard? If no then this optimisation may cause severe errors if we are using pointers. See the next code. #include <iostream> using namespace std; struct Array { unsigned char HSize, VSize; int *p; bool canBeDeleted; Array(unsigned char, unsigned char); Array(unsigned char, unsigned char, int*); Array(const Array&); Array operator[](unsigned char) const; Array& operator=(const Array&); Array& operator=(int); Array operator+(Array) const; friend ostream& operator<<(ostream&, const Array&); operator int&() {return p[0];}; ~Array(); }; Array::Array(unsigned char n, unsigned char m) { VSize = n; HSize = m; p = new int[n*m]; canBeDeleted = true; } Array::Array(unsigned char n, unsigned char m, int* p1) { VSize = n; HSize = m; p = p1; canBeDeleted = true; } Array::Array(const Array& Data) { HSize = Data.HSize; VSize = Data.VSize; p = new int[VSize*HSize]; for (int i = 0; i < Data.VSize; i++) { for (int j = 0; j < Data.HSize; j++) p[i*HSize + j] = Data.p[i*HSize + j]; } canBeDeleted = true; } Array::~Array() { if (canBeDeleted) delete []p; } Array Array::operator[](unsigned char i) const { if (i >= VSize && VSize != 1) throw 1; unsigned char size = HSize; if (VSize == 1) { size = 1; if (i >= HSize) throw 7; } Array A(1, size, p + i*size); A.canBeDeleted = false; return A; } Array& Array::operator=(int i) { if (HSize != 1 && VSize != 1) throw 2; this->p[0] = i; } Array& Array::operator=(const Array& Data) { if (VSize == Data.VSize && HSize == Data.HSize) for (int i = 0; i < VSize; i++) for (int j = 0; j < HSize; j++) p[i*HSize + j] = Data.p[i*HSize + j]; else throw 3; return *this; } Array Array::operator+(Array Data) const { if (VSize == Data.VSize && HSize == Data.HSize) for (int i = 0; i < VSize; i++) for (int j = 0; j < HSize; j++) Data.p[i*HSize + j] += p[i*HSize + j]; else throw 5; return Data; } ostream& operator<<(ostream& s, const Array& Data) { if (Data.VSize == 1 && Data.HSize == 1) { s << Data.p[0]; return s; } s << endl; for (int i = 0; i < Data.VSize; i++) { for (int j = 0; j < Data.HSize; j++) s << '\t' << Data.p[i*Data.HSize + j]; s << endl; } s << endl; return s; } int main() { Array A(3,3), E(3,3); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) { E[i][j] = i == j; A[i][j] = i + j; } cout << "A" << A << "E" << E; A[1][2] = 7; A[2] = E[1]; //it works. but should it work? A[1] + E[2]; //a problem! cout << "E" << E; } E was changed! :-( Regards Litwr