[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

question on the order in which objects created on the stack get destroyed

Yan

11/18/2008 11:21:00 PM

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;
}
-------------------
7 Answers

Paavo Helde

11/18/2008 11:40:00 PM

0

Yan <yvinogradov@gmail.com> kirjutas:

> 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;
> }
> -------------------

The objects are destructed in reverse order of construction. This is
guaranteed by the standard, otherwise it would become very hard to write
working code.

Paavo

Andrey Tarasevich

11/18/2008 11:41:00 PM

0

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

Jeff Schwab

11/19/2008 12:07:00 AM

0

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.

Andrey Tarasevich

11/19/2008 12:19:00 AM

0

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

Jeff Schwab

11/19/2008 12:44:00 AM

0

Andrey Tarasevich wrote:
> 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.

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

Andrey Tarasevich

11/19/2008 1:04:00 AM

0

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

Jeff Schwab

11/19/2008 3:22:00 PM

0

Andrey Tsarevich wrote:
> 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.

Thank you. I somehow (mistakenly) assumed the "in the same translation
unit" (which I now see in 3.6.2) also applied to termination.