Jump to content
  • Advertisement
Sign in to follow this  
255

Lifetime of stack objects in C++

This topic is 4789 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
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
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
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!