home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sun-barr!cs.utexas.edu!devnull!rgp
- From: rgp@mpd.tandem.com (Ramon Pantin)
- Newsgroups: comp.lang.c++
- Subject: Re: Destructors and exit()
- Message-ID: <2384@devnull.mpd.tandem.com>
- Date: 4 Sep 92 19:08:12 GMT
- References: <1992Sep3.220301.16983@murdoch.acc.Virginia.EDU> <rmartin.715615353@thor>
- Sender: news@devnull.mpd.tandem.com
- Organization: Tandem Computers, Micro-Products Division
- Lines: 57
-
- In article <rmartin.715615353@thor> rmartin@thor.Rational.COM (Bob Martin) writes:
- >main()
- >{
- > MyClass A;
- > static MyClass B;
- >
- > exit();
- >}
- >
- >
- >The destructor for B should be invoked. But the destructor for A
- >probably will not. I say probably, because a clever compiler might be
- >able to figure out that, upon exit, the enclosing stack frames should
- >be destroyed.
-
- IMHO, it would be incorrect for a compiler to "know" anything about
- exit (exit is part of the C library, not part of the language, "exit"
- is not a keyword). A call to the exit function has never triggered
- destructions of automatic objects and should never do. The block where
- the object was constructed is not being exited when the exit function
- is called so it is incorrect to destroy the object. Doing so would
- invalidate valid code such as:
-
- #include <stdlib.h>
- class A {
- A() { ... };
- ~A() { ... };
- };
- A *ap;
- cleanup()
- {
- if (ap)
- ap->cleanup();
- }
- main()
- {
- A a;
- atexit(cleanup);
- if (...)
- exit(1);
- ...
- ap = &a;
- ...
- exit(1);
- }
-
- Or some other code where the destruction of the automaitc object has side
- effects that are observable from outside the process. It is also possible
- in a standalone environment to have a user defined version of exit that
- doesn't cause program termination but some other behaviour (and also being
- extern "C" void exit(int); ).
-
- >And remember, never use gotos.
-
- Use breaks, continues, mid-function returns and gotos if you want to.
-
- Ramon Pantin
-