Bug 53990 - wrong "optimisation": automatic variable doesn't removed at fuction exit
Summary: wrong "optimisation": automatic variable doesn't removed at fuction exit
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.6.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2012-07-17 05:40 UTC by litwr
Modified: 2012-07-18 12:40 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description litwr 2012-07-17 05:40:01 UTC
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
Comment 1 Andrew Pinski 2012-07-17 06:00:37 UTC
>Is this optimization in C++ standard?

Yes it is the named return value optimization which is part of the C++ standard.

>        A[2] = E[1];  //it works. but should it work?

This code is valid but most likely does not do what you want it to do.
In that it creates a temporary variable (a rvalue) to hold A[2].  Then it calls operator= on that rvalue (which is valid).
Comment 3 litwr 2012-07-18 12:18:54 UTC
Thank you very much!  Excuse me this little ignorancy.  However it is a bit confusing that this allows to have code executed differently with different compilers.
Comment 4 Jonathan Wakely 2012-07-18 12:40:42 UTC
It's maybe a little confusing for beginners, until they learn how it works, but it makes certain operations up to three times faster so it's a huge benefit.

All good quality compilers will perform the optimisation, but if you don't want G++ to do it (and want your programs to be slower) you can use -fno-elide-constructors