Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ destructor & function call order

Tags:

c++

destructor

Suppose I have the following snipplet:

Foo foo;
....
return bar();

Now, does the C++ standard guarantees me that bar() will be called before foo::~Foo() ? Or is this the compiler/implementation's choice?

Thanks!

like image 445
anon Avatar asked Feb 03 '10 23:02

anon


People also ask

What is a destructor in C?

A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ). For example, the destructor for class String is declared: ~String() .

Are there destructors in C?

There is no such thing called 'constructors' and 'destructors' in C programming language or in structured languages, although there is no boundaries on defining such functions which act like them. You need to make functions which act like the constructors and destructors and then call them manually.

Why destructor is used in C?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.


2 Answers

It is guaranteed behaviour. The actual execution is unrolled as follows:

0: enter block (scope)
1: Foo::Foo()
2. evaluation of bar(); as expression in return statement
3. save result of the expression as value returned from function
4. finalize return statement to leave function to its caller (request exit from current scope)
5: exit block (scope) with call to  Foo::~Foo()

Here are some references from the standard:

  • What program execution guarantees, generally

1.9 Program execution

10 An instance of each object with automatic storage duration (3.7.2) is associated with each entry into its block.

  • The foo is of automatic storage duration and:

3.7.2 Automatic storage duration

1 Local objects explicitly declared auto or register or not explicitly declared static or extern have automatic storage duration. The storage for these objects lasts until the block in which they are created exits.

  • What is actual effect of return statement

6.6.3 The return statement

2 (...) the value of the expression is returned to the caller of the function

and

6.6 Jump statements (return belongs to jump statements)

2 On exit from a scope (however accomplished), destructors (12.4) are called for all constructed objects with automatic storage duration (3.7.2)

  • What guarantees that the effect occurs

6.7 Declaration statement

2 Variables with automatic storage duration declared in the block are destroyed on exit from the block

and

12.4 Destructors

10 Destructors are invoked implicitly (1) for a constructed object with static storage duration (3.7.1) at program termination (3.6.3), (2) for a constructed object with automatic storage duration (3.7.2) when the block in which the object is created exits (6.7)

It is not easy to grasp single idea form details scattered around all the C++ standard. Hopefully, quick overview will help you to make such analysis yourself too.

like image 131
mloskot Avatar answered Sep 22 '22 13:09

mloskot


Yes, bar() will be called before the destructor of foo.

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

The scope isn't left until the return statement is completed.

like image 27
villintehaspam Avatar answered Sep 22 '22 13:09

villintehaspam