Re: lifetime of objects (here: strings)
On Apr 10, 7:36 pm, r...@zedat.fu-berlin.de (Stefan Ram) wrote:
I still struggle with C++ memory management, so here are some
questions:
There seem to be three fundamental lifetimes of objects in C++?
- scope-based lifetime (auto variables)
- dynamic lifetime (=BBnew=AB)
- temporaries (expression based?)
There are more than that. There's static lifetime, of
course, and there's the lifetime of objects thrown as
exceptions, and there may be a few others that I've
forgotten. (IIRC, function arguments also have a special
lifetime.)
When =BBa=AB and =BBb=AB are instances of =BB::std::string=AB, what is =
the
lifetime of =BBa+b=AB?
It's a temporary, so the lifetime is until the end of the
full expression. With a couple of obvious exceptions, and
one not so obvious one, but at least until the end of the
full expression.
A tutorial page, chosen at random,
Don't choose your tutorials at random. There's a lot of
junk out there. (No comment on the link you give---I've not
seen it. But if you reall choose at random, you're more
likely to get junk than anything worthwhile.)
http://www.cprogramming.com/tutorial/string.html
does not seem to mention or answer this question. Shouldn't
this be very important for every programmer to know from the
moment he starts to use objects?
It depends on the object. For objects like std::string, all
you really need to know is the minimum lifetime, and most of
the time, not even that.
When a function returns a instance of a class, is it the
most common case that it returns a temporary, so that
this can be assumed, unless the documentation tells
something else?
By definition, the return value of a function is a
temporary.
Is it ok to return a temporary, as in the following example?
::std::string f()
{ ::std::string const a( "alpha" );
::std::string const b( "beta" );
return a + b; }
Yes.
If this is a temporary, IIRC, it is guaranteed to exist
during the evaluation of the full-expression of the call
site? So one either has to pass it to another function
there or copy it to an object with longer life time.
There's (officially) a copy. Officially, because the
standard explicitly gives the compiler the right to
eliminate it, by merging the temporary a+b with the
temporary returned from the function. Most compilers
actually do this optimization.
The tutorial also uses:
string my_string3 = my_string1 + my_string2;
This seems to be =BBcopy initialization=AB.
Formally.
Is it of any advantage to use =BBdirect initialization=AB in this
case? Which would be:
string my_string3( my_string1 + my_string2 );
Since the type of string1+string2 is the same as the target
type, the two are effectively the same.
Are the any guidelines, when to prefer copy
initialization and when to prefer direct initialization?
Do what the house coding guidelines say.
Ok, and I assume, what one can never to with a temporary
object, is to bind a reference name to it, as in:
string & my_string3( my_string1 + my_string2 );
But, this would be allowed with a const reference,
because for this special case ISO C++ extends the
lifetime?
Exact. But the effect is not transitive; the lifetime is
only extended to match the reference initialized with the
temporary, not to other references which were initialized
from that reference.
string const & my_string3( my_string1 + my_string2 );
Is this what =BBGotW #88=AB is about?
Yes.
So for the same reason, a function with a reference
parameter can not be called with a temporary, unless the
reference parameter is const?
Yes, but that has nothing to do with lifetime. It's just a
rule. The original rule allowed binding a temporary to any
reference. People got into trouble with temporaries that
were the result of implicit conversions, e.g.:
void
incr( int& i )
{
++ i ;
}
void
f()
{
unsigned j = 42 ;
incr( j ) ; // does *NOT* change j.
}
So the rule was changed.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34