Xref: utzoo comp.lang.c++:2353 gnu.g++.bug:159 Path: utzoo!attcan!uunet!tut.cis.ohio-state.edu!mailrus!ames!ucsd!orion.cf.uci.edu!paris.ics.uci.edu!glacier.ics.uci.edu!schmidt From: schmidt@glacier.ics.uci.edu Newsgroups: comp.lang.c++,gnu.g++.bug Subject: exit() and destructors Message-ID: <3197@paris.ics.uci.edu> Date: 6 Jan 89 06:29:50 GMT Sender: news@paris.ics.uci.edu Reply-To: schmidt@glacier.ics.uci.edu () Organization: University of California, Irvine - Dept of ICS Lines: 42 Hi, I came across an interesting C++ fact the other day. While I can see why this behavior make sense, it seems like it should at least be mentioned in any C++ language reference manual. Here's an example: ---------------------------------------- class foo { public: foo ( ) { printf ( "start\n" ); } ~foo ( void ) { printf ( "finish\n" ); } }; main ( ) { foo bar; // prints "start" exit ( 0 ); // doesn't print "finish", since exit() doesn't return! // replacing exit(0) with return 0 works. } ---------------------------------------- Most C programmers are probably conditioned to use exit(), rather than return, in the main() function. While this doesn't make much difference in C, it obviously make a *big* difference in C++, if objects with destructors are defined in the main() function. I don't recall this being mentioned explicitly in the C++ reference manual. Page 159 alludes to the differences with exit(), abort(), and destructors for static objects. Mention of this behavior probably belongs go in the ``Differences with C'' section. I hope this information saves people problems in the future. Doug -- schmidt@ics.uci.edu (ARPA) | Per me si va nella citta' dolente. office: (714) 856-4043 | Per me si va nell'eterno dolore. | Per me si va tra la perduta gente. | Lasciate ogni speranza o voi ch'entrate.