82

Is there a defined order in which local variables are deallocated in C++ (11) ? To be more concise: In which order will side effects of the destructors of two local variables in the same scope become visible?

e.g.:

struct X{
  ~X(){/*do something*/}
}

int main(){
   X x1;
   X x2;
   return 0;
}

Is x1 or x2 destroyed first when main returns or is the order undefined in C++11?

gexicide
  • 38,535
  • 21
  • 92
  • 152

6 Answers6

93

Within each category of storage classes (except dynamically allocated objects), objects are destructed in the reverse order of construction.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 8
    What part of the C++ standard guarantees this order? – David Grayson Nov 30 '14 at 22:42
  • 28
    @DavidGrayson A different part for each category. For objects with thread-local or static storage, see §3.6.3. For objects with auto lifetime see §6.6 (which is _not_ where one might expect to look for it). For class members and bases, see §12.4/8. For temporaries, see §12.2. – James Kanze Dec 01 '14 at 17:39
  • 18
    Temporaries do not strictly follow LIFO ordering, due to lifetime extension when a reference directly binds one. – Ben Voigt Jan 27 '16 at 20:29
  • @BenVoigt underrated comment. – UmNyobe Feb 06 '18 at 12:31
32

I. About local variables

  1. Local variables are allocated on the Stack.

  2. The Stack is based on a LIFO (Last-In-First-Out) pattern.

  3. So variables are destroyed and deallocated in the reverse order of allocation and construction.

II. About your example

Your function main() is called:

  • x1 is allocated and constructed on the Stack,
  • x2 is allocated and constructed on the Stack

and when the end of the main() function scope is reached:

  • x2 is destroyed and deallocated from the Stack,
  • x1 is destroyed and deallocated from the Stack

III. Moreover

The Stack look like this:

(Behaviour of the Stack seems more understandable with a scheme)

Stack scheme

Axel Borja
  • 3,718
  • 7
  • 36
  • 50
  • 2
    Although I don't disagree with the picture, the implementation is not pushing/popping anymore and that has been the case for ages. Instead it does one `sub %esp, ` and at exit it does `add %esp, `. So in other words all the necessary space is allocated in one swoop (only `alloca()` will dynamically reduce the stack pointer further.) – Alexis Wilke Aug 04 '19 at 03:17
  • 7
    This issue doesn't have anything to do with the existence of a stack or the direction in which things are allocated on it. It has to do with *lexical scopes* being abandoned in the reverse order of their establishment. The objects `x1` and `x2` can be allocated in the stack frame in any order. Or even not at all; e.g. they can be empty structs. – Kaz Aug 04 '19 at 03:18
  • 3
    @AlexisWilke Spooky! Comments in similar vein on answer from 2014 within seconds of each other. – Kaz Aug 04 '19 at 03:18
8

This is a Stack Data Structure behaviour, so local variables stores in Stack as LIFO (Last-In-First-Out) data structure, you can imagine that in a LIFO data structure, the last variable added to the structure must be the first one to be removed. variables are removed from the stack in the reverse order to the order of their addition.

Reza Ebrahimi
  • 3,623
  • 2
  • 27
  • 41
4

They are destroyed in reverse allocation order, see this SO question. In this case, this means that x2 will be destroyed before x1.

Community
  • 1
  • 1
PrisonMonkeys
  • 1,199
  • 1
  • 10
  • 20
3

https://isocpp.org/wiki/faq/dtors#order-dtors-for-locals

isocpp.org has a FAQ about this topic and it says it does in a reverse order. I think that the official site didn't exist when this original question had been asked though.

z3moon
  • 131
  • 6
  • Nice link. It provides short answers to a lot of destruction order related questions by an authoritative source. – Jakob Stark Mar 02 '22 at 12:40
2

They will be destroyed following a reverse order of their construction.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90