destruction of a global variable?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2008
Posts: 408
Reputation: sciwizeh is on a distinguished road 
Solved Threads: 22
sciwizeh's Avatar
sciwizeh sciwizeh is offline Offline
Posting Pro in Training

destruction of a global variable?

 
0
  #1
Apr 10th, 2009
Hello, I have a question, when does a global variable get destructed?

ex.
  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?
My site, random PM's from people I haven't hear from before will be DELETED
"If people are good only because they fear punishment, and hope for reward, then we are a sorry lot indeed.",
"If we knew what it was we were doing, it would not be called research, would it? "
-Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 210
Reputation: rahul8590 is on a distinguished road 
Solved Threads: 10
rahul8590's Avatar
rahul8590 rahul8590 is offline Offline
Posting Whiz in Training

Re: destruction of a global variable?

 
0
  #2
Apr 11th, 2009
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 .
<?php
$data = $_POST['data'];
if(empty($data)) {
echo "byte me" ; }
?>
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: destruction of a global variable?

 
0
  #3
Apr 11th, 2009
simple::~simple is a destructor and is called automatically when the object is being destructed, you can never call it yourself ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 24
Reputation: boblied is an unknown quantity at this point 
Solved Threads: 9
boblied boblied is offline Offline
Newbie Poster

Re: destruction of a global variable?

 
0
  #4
Apr 11th, 2009
Originally Posted by sciwizeh View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: destruction of a global variable?

 
0
  #5
Apr 11th, 2009
Originally Posted by sciwizeh View Post
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 ...
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 374
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: destruction of a global variable?

 
0
  #6
Apr 11th, 2009
You can easily test that, just put some messages in the destructor and some in the program itself to see its place right?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: destruction of a global variable?

 
0
  #7
Apr 11th, 2009
Originally Posted by Clockowl View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 309
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 18
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: destruction of a global variable?

 
0
  #8
Apr 11th, 2009
Originally Posted by ArkM View Post
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.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: destruction of a global variable?

 
0
  #9
Apr 11th, 2009
Originally Posted by NicAx64 View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC