| | |
destruction of a global variable?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hello, I have a question, when does a global variable get destructed?
ex.
when will simple::~simple() be called? after the execution of main finishes?
ex.
c++ Syntax (Toggle Plain Text)
#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) }
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
"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
•
•
Join Date: Mar 2009
Posts: 24
Reputation:
Solved Threads: 9
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.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
•
•
•
•
You can easily test that, just put some messages in the destructor and some in the program itself to see its place right?
•
•
•
•
Constructors and destructors for static objects can access these objects to read input from stdin or write output to
stdout or stderr.
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 !
•
•
•
•
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 ArkM; Apr 11th, 2009 at 4:51 pm.
![]() |
Other Threads in the C++ Forum
- Previous Thread: bug fix
- Next Thread: plz plz help me :'(
| Thread Tools | Search this Thread |
api array beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text text-file tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






