public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/37487]  New: invalid return by value optimization
@ 2008-09-11 21:00 khoaduynguyen at gmail dot com
  2008-09-11 21:01 ` [Bug c++/37487] " khoaduynguyen at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: khoaduynguyen at gmail dot com @ 2008-09-11 21:00 UTC (permalink / raw)
  To: gcc-bugs

For return by value optimization, instead of calling the copy constructor or
the assignment operator, gcc decides to optimize and push the address of the
lhs of the expression into the rhs.

Foo createFoo() { Foo f2; return f2; }
int main(){
  Foo f1 = createFoo();
  return 0;
}

OPTIMIZED TO
void createFoo( Foo& f2 ) {...} 
int main() {
  Foo f1;
  createFoo(f1);
  return 0;
}

This is a problem when you want to explicitly define the copy constructor or
assignment operator to do something other than deep copying.


-- 
           Summary: invalid return by value optimization
           Product: gcc
           Version: 3.4.6
            Status: UNCONFIRMED
          Severity: blocker
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: khoaduynguyen at gmail dot com


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


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

* [Bug c++/37487] invalid return by value optimization
  2008-09-11 21:00 [Bug c++/37487] New: invalid return by value optimization khoaduynguyen at gmail dot com
@ 2008-09-11 21:01 ` khoaduynguyen at gmail dot com
  2008-09-11 21:03 ` khoaduynguyen at gmail dot com
  2008-09-11 21:04 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: khoaduynguyen at gmail dot com @ 2008-09-11 21:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from khoaduynguyen at gmail dot com  2008-09-11 21:00 -------
Created an attachment (id=16299)
 --> (http://gcc.gnu.org/bugzilla/attachment.cgi?id=16299&action=view)
Reproduction


-- 


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


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

* [Bug c++/37487] invalid return by value optimization
  2008-09-11 21:00 [Bug c++/37487] New: invalid return by value optimization khoaduynguyen at gmail dot com
  2008-09-11 21:01 ` [Bug c++/37487] " khoaduynguyen at gmail dot com
@ 2008-09-11 21:03 ` khoaduynguyen at gmail dot com
  2008-09-11 21:04 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: khoaduynguyen at gmail dot com @ 2008-09-11 21:03 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from khoaduynguyen at gmail dot com  2008-09-11 21:02 -------
-bash-3.1$ gcc -v
Using built-in specs.
Target: x86_64-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,obj-c++,java,fortran,ada --enable-java-awt=gtk
--disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre --with-cpu=generic
--host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20070626 (Red Hat 4.1.2-14)

[knguyen@rnd01 ~]$ g++ test2.cpp
[knguyen@rnd01 ~]$ ./a.out 
In main
Base(int), id=0, this=0x7ffff946c390
Base(const Base& k), id=1, this=0x7ffff946c2e0
Derived(const Base&), id=1, this=0x7ffff946c2e0
Test1 -- d.id=0


Base(int), id=2, this=0x7ffff946c3a0
Base(const Base& k), id=3, this=0x7ffff946c260
Derived(const Base&), id=3, this=0x7ffff946c260
Test2 -- d2.id=3


int Derived.test()
Base(int), id=4, this=0x7ffff946c1e0
Derived(), id=4, this0x7ffff946c1e0
Test3 -- d3.id=4


int Derived.test()
Base(int), id=5, this=0x7ffff946c380
Test4 -- b4.id=5, &b4=0x7ffff946c380


Base(), id=6, this=0x7ffff946c370
Base(), id=7, this=0x7ffff946c360
Base Operator=(const Base&), id=8, this=0x7ffff946c360
Base(const Base& k), id=9, this=0x7ffff946c160
Derived(const Base&), id=9, this=0x7ffff946c160
Test4 -- b4.id=9, &b4=0x7ffff946c160


-- 

khoaduynguyen at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|invalid return by value     |invalid return by value
                   |optimization                |optimization


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


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

* [Bug c++/37487] invalid return by value optimization
  2008-09-11 21:00 [Bug c++/37487] New: invalid return by value optimization khoaduynguyen at gmail dot com
  2008-09-11 21:01 ` [Bug c++/37487] " khoaduynguyen at gmail dot com
  2008-09-11 21:03 ` khoaduynguyen at gmail dot com
@ 2008-09-11 21:04 ` pinskia at gcc dot gnu dot org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-09-11 21:04 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2008-09-11 21:02 -------
This is allowed by the standard.  THe compiler can elide constructors as
defined by the C++ standard.   If you want not this behavior use
-fno-elide-constructors.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


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


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

end of thread, other threads:[~2008-09-11 21:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-11 21:00 [Bug c++/37487] New: invalid return by value optimization khoaduynguyen at gmail dot com
2008-09-11 21:01 ` [Bug c++/37487] " khoaduynguyen at gmail dot com
2008-09-11 21:03 ` khoaduynguyen at gmail dot com
2008-09-11 21:04 ` pinskia at gcc dot gnu dot 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).