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?

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? ;)

This article has been dead for over six months. Start a new discussion instead.