• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Do locals destruct in the reverse order they construct?

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
shrif
Asked:
shrif
  • 7
  • 5
  • 4
  • +3
1 Solution
 
nietodCommented:
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
 
pagladasuCommented:
With very early versions of C++, the objects would be destroyed in the order in which they were created.

0
 
shrifAuthor Commented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pagladasuCommented:
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
 
nietodCommented:
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
 
alexoCommented:
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
 
Answers2000Commented:
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
 
nietodCommented:
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
 
shrifAuthor Commented:
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
 
shrifAuthor Commented:
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
 
nietodCommented:
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
 
shrifAuthor Commented:
nietod, you're right.  Let's leave this one sit.  I'll see if I can get something conclusive from other sources.
0
 
nietodCommented:
I'll try to se if Yonat can help on this.  (I think Bjarne Stroustrup goes to her for help.)
0
 
yonatCommented:
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
 
alexoCommented:
>> 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
 
nietodCommented:
That is good to know.  Thanks for the web addresses too!  (But you don't know a !@#$$ thing about optimizers.)
0
 
yonatCommented:
Oops - i missed that - thanks!
0
 
shrifAuthor Commented:
okay okay!  I think I was always convinced.  I just needed the reference so that others would be convinced.

Thanks.

0
 
alexoCommented:
>> But you don't know a !@#$$ thing about optimizers.
Go back to the question, read my comments and weep.
0
 
nietodCommented:
Sorry, no tears.  
0
 
alexoCommented:
>> 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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 7
  • 5
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now