943,657 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1452
  • C++ RSS
Apr 10th, 2009
0

destruction of a global variable?

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

ex.
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. class simple {
  4. public:
  5. int *i;
  6. simple(int ni){
  7. i = new int(ni);
  8. }
  9. ~simple(){
  10. delete i;
  11. }
  12. }
  13. simple simple1(10);
  14. //for simplicity no command line
  15. int main(){
  16. cout<<*(simple.i)
  17. }
when will simple::~simple() be called? after the execution of main finishes?
Reputation Points: 73
Solved Threads: 22
Posting Pro in Training
sciwizeh is offline Offline
423 posts
since Jun 2008
Apr 11th, 2009
0

Re: destruction of a global variable?

Quote ...
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 .
Reputation Points: 92
Solved Threads: 20
Posting Whiz
rahul8590 is offline Offline
351 posts
since Mar 2009
Apr 11th, 2009
0

Re: destruction of a global variable?

simple::~simple is a destructor and is called automatically when the object is being destructed, you can never call it yourself ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 11th, 2009
0

Re: destruction of a global variable?

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
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.
Reputation Points: 36
Solved Threads: 9
Newbie Poster
boblied is offline Offline
24 posts
since Mar 2009
Apr 11th, 2009
0

Re: destruction of a global variable?

Click to Expand / Collapse  Quote originally posted by sciwizeh ...
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 ...
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Apr 11th, 2009
0

Re: destruction of a global variable?

You can easily test that, just put some messages in the destructor and some in the program itself to see its place right?
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
Apr 11th, 2009
0

Re: destruction of a global variable?

Click to Expand / Collapse  Quote originally posted by Clockowl ...
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:
Quote ...
Constructors and destructors for static objects can access these objects to read input from stdin or write output to
stdout or stderr.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Apr 11th, 2009
0

Re: destruction of a global variable?

Click to Expand / Collapse  Quote originally posted by ArkM ...
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.
Last edited by NicAx64; Apr 11th, 2009 at 3:17 pm.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Apr 11th, 2009
0

Re: destruction of a global variable?

Click to Expand / Collapse  Quote originally posted by NicAx64 ...
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?
Last edited by ArkM; Apr 11th, 2009 at 4:51 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: bug fix
Next Thread in C++ Forum Timeline: Can i modify final font ouput??





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC