954,157 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

destruction of a global variable?

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?

sciwizeh
Posting Pro in Training
451 posts since Jun 2008
Reputation Points: 77
Solved Threads: 23
 
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 .

rahul8590
Posting Whiz
351 posts since Mar 2009
Reputation Points: 92
Solved Threads: 20
 

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

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 
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.

boblied
Newbie Poster
24 posts since Mar 2009
Reputation Points: 36
Solved Threads: 9
 
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 ...

tux4life
Nearly a Posting Maven
2,350 posts since Feb 2009
Reputation Points: 2,134
Solved Threads: 243
 

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

Clockowl
Posting Whiz
376 posts since May 2008
Reputation Points: 69
Solved Threads: 28
 
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.

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 
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.

NicAx64
Posting Pro
534 posts since Mar 2009
Reputation Points: 86
Solved Threads: 43
 
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? ;)

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You