Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the destruction order between global object and static variable? [duplicate]

Tags:

c++

Possible Duplicate:
Object destruction in C++

Suppose we have two classes, one is called Array and the other one is called MessageOnExit.

Assume the Array class has a static data member called counter.

Here are the classes:

class Array {
public:
    static int counter;
    Array(){counter++;}
    ~Array(){counter--;}
};

class MessageOnExit {
public:
    ~MessageOnExit(){cout << Array::counter;}
};

The following is the "main" using these classes:

// static variable definition
int Array::counter;
// global object definition
MessageOnExit message;

void main() {
    Array a1, a2;
    Array a3(a1);
}

The first two declarations will change counter to be 2 ensues two calls of the Empty/Default constructor The third declaration won't change the counter because the Default copy constructor will be called.

Now when the main execution is done, the value of the static member counterwill be -1 ensues the destructors calls (after a1, a2 and a3 are desroyed), and then the message destructor will be called, printing this value (-1).

My question is how do we know the static member counter is still alive and available when the message destructor is being called.

Also if I change the static/global object/variable definition order, that is, message is defined before counter, the value remains -1.

To summarize, no matter what changes I do, when the program terminates the message destructor success to access the static member counter and I don't understand why is that. As far as I understand if the global object message is defined first its destructor will be called last and therefore the counter value won't be available for its destructor.

Thanks in advance! Guy

like image 825
SyndicatorBBB Avatar asked Jan 16 '13 16:01

SyndicatorBBB


People also ask

What is the difference between global variable and static global variable?

A global variable can be accessed from anywhere inside the program while a static variable only has a block scope. So, the benefit of using a static variable as a global variable is that it can be accessed from anywhere inside the program since it is declared globally.

When global variable will be destroyed?

Global variables are created when the program starts, and destroyed when it ends. This is called static duration. Variables with static duration are sometimes called static variables. Unlike local variables, which are uninitialized by default, variables with static duration are zero-initialized by default.

Which is the destructor of a global object called?

When is the destructor of a global object called? Explanation: This is because the lifespan of global object is from start of the program, till the end of the program. And hence program end is the end of global object too.

How do you destroy a static object in C++?

Static objects are destroyed when the program stops running. Variables defined outside a function or by using the keyword static have static storage duration. They persist for the entire running time of a program.


1 Answers

Standard section 3.8 says:

The lifetime of an object of type T ends when:

  • if T is a class type with a non-trivial destructor (12.4), the destructor call starts, or

  • the storage which the object occupies is reused or released.

Now int is not a class type, and since Array::counter has static storage duration, its storage is never released. So the lifetime of Array::counter never ends, and you can safely access it from any destructor you want!

like image 115
aschepler Avatar answered Nov 15 '22 08:11

aschepler