Sign in to follow this  
255

Lifetime of stack objects in C++

Recommended Posts

255    368
Is an object created on the stack guaranteed (by the standard) to be have its destructor called exactly at the end of the scope it was created in?

Share this post


Link to post
Share on other sites
TeMPOraL    136
Yes, it is. If you create an object on the stack, it will be destroyed just when the program leaves the scope, either is this because of an thrown exception, return statement or end of the block ('}'). And when the object is destroyed, it's destructor is called.

Share this post


Link to post
Share on other sites
Nemesis2k2    1045
Not nessicarily. If a function returns a value, the compiler can, at its discression, choose to keep that variable alive until the expression in which the function was called is complete I believe. Eg, consider the following:

int Foo();

int var = Foo() * 2;




The memory which contains the value returned from Foo(), assuming Foo() is returning a variable which is on the stack, may be kept around in order to evaluate the value of var directly, without copying that return value into a new location first.

Share this post


Link to post
Share on other sites
TeMPOraL    136
Quote:
Original post by Nemesis2k2
Not nessicarily. If a function returns a value, the compiler can, at its discression, choose to keep that variable alive until the expression in which the function was called is complete I believe. Eg, consider the following:

*** Source Snippet Removed ***

The memory which contains the value returned from Foo(), assuming Foo() is returning a variable which is on the stack, may be kept around in order to evaluate the value of var directly, without copying that return value into a new location first.


Well... yes. I didn't thought about return-value optimalization :) . But this applies only to the returned object, and only if it is temporary - without a name. All named objects, and especially the ones that are not returned, are utterly destroyed when leaving their scope.

Share this post


Link to post
Share on other sites
255    368
Thanks for the clarification. This time I only need to worry about an object existing at least until the end of a block, though.

Share this post


Link to post
Share on other sites
mgarriss    154
Quote:
Original post by Nemesis2k2
Not nessicarily. If a function returns a value, the compiler can, at its discression, choose to keep that variable alive until the expression in which the function was called is complete I believe. Eg, consider the following:

*** Source Snippet Removed ***

The memory which contains the value returned from Foo(), assuming Foo() is returning a variable which is on the stack, may be kept around in order to evaluate the value of var directly, without copying that return value into a new location first.

true but it should be noted that if an object has a destructor with side-effects it is guaranteed by the standard that it's destructor will be called. see 3.7.2 paragraph 3 in the standard.

_ugly

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this