6

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

Community
  • 1
  • 1
SyndicatorBBB
  • 1,757
  • 2
  • 26
  • 44
  • Take a look at [this](http://stackoverflow.com/questions/12728535/will-global-static-variables-be-destroyed-at-program-end), [this](http://stackoverflow.com/questions/2204608/does-c-call-destructors-for-global-and-class-static-variables) and [this](http://stackoverflow.com/questions/2087600/is-a-c-destructor-guaranteed-not-to-be-called-until-the-end-of-the-block). – PaperBirdMaster Jan 16 '13 at 16:36
  • Using an object after it has been destroyed is not a diagnosed error, its undefined behavior, so anything might happen -- the code might appear to work fine one day and break the next. – Chris Dodd Jan 16 '13 at 16:46
  • I edited away all the bold text because it had the effect of emphasizing parts of the text that shouldn't have been. I either removed it or changed them to code-quotes. You can add back bolding in some places if you feel it's really needed. – Mark B Jan 16 '13 at 16:46
  • Reopen this question. The important point here is that a static object with trivial destructor is never destroyed; see aschepler's answer. That is not stated clearly in the answers to the other questions, – Johan Råde Jan 17 '13 at 10:16

1 Answers1

7

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!

Community
  • 1
  • 1
aschepler
  • 70,891
  • 9
  • 107
  • 161