public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit
@ 2012-07-17  5:40 vol.litwr at gmail dot com
  2012-07-17  6:00 ` [Bug c++/53990] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: vol.litwr at gmail dot com @ 2012-07-17  5:40 UTC (permalink / raw)
  To: gcc-bugs

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: unassigned@gcc.gnu.org
        ReportedBy: vol.litwr@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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53990] wrong "optimisation": automatic variable doesn't removed at fuction exit
  2012-07-17  5:40 [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit vol.litwr at gmail dot com
@ 2012-07-17  6:00 ` pinskia at gcc dot gnu.org
  2012-07-17  8:03 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-07-17  6:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53990

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|wrong-code                  |accepts-invalid
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
           Severity|major                       |normal

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 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).


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53990] wrong "optimisation": automatic variable doesn't removed at fuction exit
  2012-07-17  5:40 [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit vol.litwr at gmail dot com
  2012-07-17  6:00 ` [Bug c++/53990] " pinskia at gcc dot gnu.org
@ 2012-07-17  8:03 ` redi at gcc dot gnu.org
  2012-07-18 12:19 ` vol.litwr at gmail dot com
  2012-07-18 12:40 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-17  8:03 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53990

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-07-17 08:02:56 UTC ---
See http://en.wikipedia.org/wiki/Copy_elision and
http://en.wikipedia.org/wiki/Return_value_optimization


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53990] wrong "optimisation": automatic variable doesn't removed at fuction exit
  2012-07-17  5:40 [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit vol.litwr at gmail dot com
  2012-07-17  6:00 ` [Bug c++/53990] " pinskia at gcc dot gnu.org
  2012-07-17  8:03 ` redi at gcc dot gnu.org
@ 2012-07-18 12:19 ` vol.litwr at gmail dot com
  2012-07-18 12:40 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: vol.litwr at gmail dot com @ 2012-07-18 12:19 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53990

--- Comment #3 from litwr <vol.litwr at gmail dot com> 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.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/53990] wrong "optimisation": automatic variable doesn't removed at fuction exit
  2012-07-17  5:40 [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit vol.litwr at gmail dot com
                   ` (2 preceding siblings ...)
  2012-07-18 12:19 ` vol.litwr at gmail dot com
@ 2012-07-18 12:40 ` redi at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2012-07-18 12:40 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53990

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2012-07-18 12:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-17  5:40 [Bug c++/53990] New: wrong "optimisation": automatic variable doesn't removed at fuction exit vol.litwr at gmail dot com
2012-07-17  6:00 ` [Bug c++/53990] " pinskia at gcc dot gnu.org
2012-07-17  8:03 ` redi at gcc dot gnu.org
2012-07-18 12:19 ` vol.litwr at gmail dot com
2012-07-18 12:40 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).