Lifetime of stack objects in C++

Started by
5 comments, last by mgarriss 18 years, 10 months ago
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?
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.
Tempus FugitI'm an Object Oriented Programmer ;).My site [placeholder; rest is under development]Currently working on: Holodeck 2D Engine
Thank you.
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.
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.
Tempus FugitI'm an Object Oriented Programmer ;).My site [placeholder; rest is under development]Currently working on: Holodeck 2D Engine
Thanks for the clarification. This time I only need to worry about an object existing at least until the end of a block, though.
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

This topic is closed to new replies.

Advertisement