Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question on the order in which objects created on the stack getdestroyed

Reply
Thread Tools

question on the order in which objects created on the stack getdestroyed

 
 
Yan
Guest
Posts: n/a
 
      11-18-2008
Can I rely on the order in which destructors for objects 'a' and 'b'
are called in the attached code or it's up to the compiler to decide?

Thank you.

-------------------
class A {};
class B {};

void f() {
A a;
B b;
}
-------------------
 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-18-2008
Yan wrote:
> Can I rely on the order in which destructors for objects 'a' and 'b'
> are called in the attached code or it's up to the compiler to decide?
>
> Thank you.
>
> -------------------
> class A {};
> class B {};
>
> void f() {
> A a;
> B b;
> }
> -------------------


Yes. In C++ static and automatic objects are destroyed in the reverse
order of their creation.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
 
 
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-19-2008
Jeff Schwab wrote:
> Andrey Tarasevich wrote:
>> Yan wrote:
>>> Can I rely on the order in which destructors for objects 'a' and 'b'
>>> are called in the attached code or it's up to the compiler to decide?
>>>
>>> Thank you.
>>>
>>> -------------------
>>> class A {};
>>> class B {};
>>>
>>> void f() {
>>> A a;
>>> B b;
>>> }
>>> -------------------

>>
>> Yes. In C++ static and automatic objects are destroyed in the reverse
>> order of their creation.

>
> Given that they are in the same translation unit.


No. The global order or destruction is always the reverse of the global
order of construction, whatever the latter might be. It is true that the
order of construction [of static objects] is not specified across
translation units, but once some (any) global construction order took
place during the actual execution of the program, the order of
destruction is required to be the exact reverse of it. Globally, i.e.
across all translation units.

--
Best regards,
Andrey Tarasevich

 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      11-19-2008
Jeff Schwab wrote:
>>
>> No. The global order or destruction is always the reverse of the
>> global order of construction, whatever the latter might be. It is true
>> that the order of construction [of static objects] is not specified
>> across translation units, but once some (any) global construction
>> order took place during the actual execution of the program, the order
>> of destruction is required to be the exact reverse of it. Globally,
>> i.e. across all translation units.

>
> D'oh. I stand corrected. Do you know where the standard covers this?
> I'm having a little trouble finding it.


I believe "3.6.3 Termination" covers it in the very first paragraph.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ compilers have one stack for local variables and return addresses and then another stack for array allocations on the stack. Casey Hawthorne C Programming 3 11-01-2009 08:23 PM
Rerieving array of Java Objects which are created in a JNI method markryde@gmail.com Java 3 12-23-2005 07:46 AM
are the objects created in the stack guarranted to have been created? jimjim C++ 12 06-03-2005 12:57 PM
In which order are files looked for when loaded/requierd - and what'sthe order of suffixes? Stephan Kämper Ruby 2 01-18-2004 02:07 PM
postback question, is the order in which objects are rendered and returned controllable? e ASP .Net 1 11-19-2003 10:13 PM



Advertisments