RE: [ROOT] Seg.Violation on return TGraph

From: Nick West (n.west1@physics.ox.ac.uk)
Date: Thu Mar 13 2003 - 08:55:40 MET


Hi Christian,

thanks for your interesting and enlightening emails; I always look forward
to them.  Just in case anyone else tries your example on more recent gcc
compilers, I will report that, under gcc 3.2 I get, for the first case:-

+ 1: plain object
- 1: foo::foo(int)
==> Notice the extra copy above?

and only when I went back to 2.91, do I get:-

+ 1: plain object
- 1: foo::foo(int)
- 1: foo::foo(const foo&)
- 1: foo::~foo
==> Notice the extra copy above?

So, as your second mail explained:-

>    Thing f() { 
>            Thing t;
>	    return t;
>    }
>    
>    Thing t2 = f();
>
>  Here t does not need to be copied when returning from f. The return
>  value of f may be constructed directly into the object t2.]

So the casual programmer now goes unpunished for the profligate use of
temporary objects with gcc.  Of course it's good that the compiler writer
seeks to optimise the compiled code but there is the danger that the
programmer will become sloppy member of the partnership.  What if several
local objects are created and then one chosen for return?  Clever compilers
cannot help then.

Cheers,

Nick.



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:10 MET