Hello, I have a question, when does a global variable get destructed?

ex.

#include <iostream>
using namespace std;
class simple {
    public:
    int *i;
    simple(int ni){
        i = new int(ni);
    }
    ~simple(){
        delete i;
    }
}
simple simple1(10);
//for simplicity no command line
int main(){
     cout<<*(simple.i)
}

when will simple::~simple() be called? after the execution of main finishes?

Dani AI

Generated

Short answer: yes, your non-local object simple1 (it has static storage duration) is destroyed during normal program termination. Within a single translation unit, such objects are destroyed in the reverse order of their completed construction. Across translation units the order is unspecified, which is the root of the well-known static initialization order fiasco; prefer the construct-on-first-use idiom (a function-local static) if one static depends on another. Since C++11, initialization of function-local statics is thread-safe. cppreference: storage duration, cppreference: SIOF.

As noted, returning from main behaves like calling std::exit: it will run destructors for objects with static storage duration in the correct reverse order. But several termination paths skip those destructors: std::quick_exit and std::_Exit do not run static destructors (only at_quick_exit handlers run for the former). That distinction is easy to observe:

#include <cstdlib>
#include <iostream>
struct Tracker { ~Tracker(){ std::cout << "~Tracker\n"; } };
Tracker g;

int main() {
    std::exit(0);    // g's destructor runs
    // std::_Exit(0); // uncomment: g's destructor would NOT run
}

cppreference: std::exit, cppreference: std::_Exit, cppreference: std::quick_exit.

On ’s point about iostreams: the standard library arranges for the iostream singletons (std::cout, etc.) to be constructed before main (via std::ios_base::Init) and not destroyed during program execution, so writing to std::cout from static destructors is intended to work portably. cplusplus.com: ios_base::Init, WG21 LWG issue 369.

One last safety note for your class design: if a type owns a new’d resource, follow the rule of three/five or prefer std::unique_ptr<int> (or simply store int by value) to avoid double-delete hazards.

Recommended Answers

All 8 Replies

Hello, I have a question, when does a global variable get destructed?

well i guess when the program terminates , the global variable is freed by the OS .

simple::~simple is a destructor and is called automatically when the object is being destructed, you can never call it yourself ...

Hello, I have a question, when does a global variable get destructed?

The compiler arranges for code to get executed both before main() is called and after it returns. Before main(), global constructors are called. After main(), global destructors are called in opposite order of their construction.

when will simple::~simple() be called? after the execution of main finishes?

When the execution of main finishes, just before the program exits the destructor will be called ...

You can easily test that, just put some messages in the destructor and some in the program itself to see its place right? ;)

You can easily test that, just put some messages in the destructor and some in the program itself to see its place right? ;)

Yes, that's true. The C++ Standard requires that objects associated with std::cin, std::cout and std::cerr are destroyes after destruction of user-defined objects with static duration:

Constructors and destructors for static objects can access these objects to read input from stdin or write output to
stdout or stderr.

Yes, that's true. The C++ Standard requires that objects associated with std::cin, std::cout and std::cerr are destroyes after destruction of user-defined objects with static duration:

standard input , output and the error is a operating system concept, as in linux and they are never destructed , but C++ object associated with them get destructed after destruction of user-defined objects with static duration.

standard input , output and the error is a operating system concept, as in linux and they are never destructed , but C++ object associated with them get destructed after destruction of user-defined objects with static duration.

It seems you did not understand my post. What for you reprint the same sentence? ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.