Solved

Do locals destruct in the reverse order they construct?

Posted on 1998-12-07
21
309 Views
Last Modified: 2011-09-20
Here's sample code:

#include <iostream>
using namespace std;

struct C {
    int x;
    C(int x) : x(x) {}
    ~C() { cout << x; }
};

void main()
{
    C c1(1), c2(2), c3(3);
}


I get the destructor called in the reverse order.  Anyone know if I can rely on that?

Output:
321

I need a reference to the C++ standard which proves this.
0
Comment
Question by:shrif
  • 7
  • 5
  • 4
  • +3
21 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1179479
You are guaranteed the order in which global objects are destroyed (bottom to top of translation unit) and you are guarantted the order in which members are destroyed (end to start of class definition.)  But I can find no reference that indicates the order in which non-static locals  are destroyed.    All the same.  I'm very confident that it is specified in the standard and it is in reverse order, otherwise you would open up to a huge number of possible problems.
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179480
With very early versions of C++, the objects would be destroyed in the order in which they were created.

0
 
LVL 1

Author Comment

by:shrif
ID: 1179481
Thanks a lot for your messages, however, I really do require a reference to the standard that mentions this, or, something else in the standard that directly implies this.

Thanks again.
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179482
I am posting this as an answer:
Here is a line from the ISO/ANSI C++ Draft April 1995. This is under the topic of destructors.(Point No 5)

5. Bases and members are destroyed in reverse order of their construction
(see  _class.base.init_).   Destructors  for  elements of an array are
called in reverse order of their construction (see _class.init_).


0
 
LVL 22

Expert Comment

by:nietod
ID: 1179483
Pagladasu, that isn't what the question asked.  What order are LOCALS destroyed in?  It think that also has to be in reverse order, but I can't find it specified.  
0
 
LVL 11

Expert Comment

by:alexo
ID: 1179484
Locals will probably be destroyed in reverse order of construction.  The ARM says so about array elements.  MSVC docs say so about automatic objects (although that is not a good reference).
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1179485
If the ARM doesn't say that locals are destroyed in reverse order, then it's not part of the standard.  I think therefore it is a false assumption to assume all compilers will destroy in reverse order (even if some/most compilers do)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179486
I really suspect that somewhere this is specifed as the standard.  There are lots of times when you might create a pair of local objects where the 2nd object is initialized with and maintains a pointer or reference to the first object.  You can do this because you are guaranteed that locals are constructed in order.  But since the 2nd object might need to use the 1st throughout its life, you should be guaranteed that they will be destroyed in reverse order.  I find it hard to believe that that would not make it into the standard.
0
 
LVL 1

Author Comment

by:shrif
ID: 1179487
I am very sorry, but I cannot accept this answer.  I am looking for a reference to the C++ standard that talks about in which order locals are destroyed.  I believe, with everyone here, that locals are destroyed in the reverse order they are created.  However, the answer you've submitted is talking about class members and arrays, which isn't what I'm after.

My original thinking was along the lines of what nietod is indicating.  The whole issue of allocating resources in the constructor and freeing them in the destructor wouldn't work very well if the destruction order of locals weren't defined.
0
 
LVL 1

Author Comment

by:shrif
ID: 1179488
sorry about that.  seems like not worth it for just 20 points.  see text above.

I figured this wasn't worth a lot because it was a matter of looking something up, but if someone has the answer and needs more points, I can up it.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
ID: 1179489
I think the problem is that no one has the answer.  Which I have to admit is suspicious, especially since we can all find info about destruction of other items.  But I still beleive that it must be specified.  I guess if you wnat to be absolutely safe you can always use { } to control the scope and order of destruction.
0
 
LVL 1

Author Comment

by:shrif
ID: 1179490
nietod, you're right.  Let's leave this one sit.  I'll see if I can get something conclusive from other sources.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179491
I'll try to se if Yonat can help on this.  (I think Bjarne Stroustrup goes to her for help.)
0
 
LVL 5

Expert Comment

by:yonat
ID: 1179492
1. As far as I remember, in all the compilers I used the destruction order of locals was indeed the reverse of the construction order.

2. I don't see anything about this in the standard. This is probably intentional - less specification means more chances for optimization. So my conclusion would be not to rely on this a general rule.

3. I doubt Bjarne Stroustrup needs my help, but he and other C++ biggies do read news:comp.lang.c++.moderated - You can try asking this question there. Not that I'll refuse to help him if I can... ;-)
0
 
LVL 11

Accepted Solution

by:
alexo earned 20 total points
ID: 1179493
>> If the ARM doesn't say [...], then it's not part of the standard.
A2K, the ARM is outdated since the ISO/ANSI standard has been accepted.

OK, the answer is a definite "yes".

http://www.cygnus.com/misc/wp/dec96pub/stmt.html#stmt.jump (draft standard) says:

On exit from a scope (however accomplished), destructors are called for all constructed objects with automatic storage duration (named objects or temporaries) that are declared in that scope, in the reverse order of their declaration.

http://www.cerfnet.com/~mpcline/c++-faq-lite/dtors.html#[11.2] (C++ FAQ lite) says:

11.2] What's the order that local objects are destructed?

In reverse order of construction: First constructed, last destructed.

In the following example, b's destructor will be executed first, then a's destructor:

    void userCode()
    {
      Fred a;
      Fred b;
      // ...
    }

Convinced?
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179494
That is good to know.  Thanks for the web addresses too!  (But you don't know a !@#$$ thing about optimizers.)
0
 
LVL 5

Expert Comment

by:yonat
ID: 1179495
Oops - i missed that - thanks!
0
 
LVL 1

Author Comment

by:shrif
ID: 1179496
okay okay!  I think I was always convinced.  I just needed the reference so that others would be convinced.

Thanks.

0
 
LVL 11

Expert Comment

by:alexo
ID: 1179497
>> But you don't know a !@#$$ thing about optimizers.
Go back to the question, read my comments and weep.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1179498
Sorry, no tears.  
0
 
LVL 11

Expert Comment

by:alexo
ID: 1179499
>> Sorry, no tears.
I can recommend good eye drops.

This is getting out of hand.  Let's stop polluting the EE DB.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now