www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/09/16/00:45:22

Date: Mon, 16 Sep 1996 14:18:00 +1000 (EST)
From: John Joseph Newbigin <079519 AT bud DOT cc DOT swin DOT edu DOT au>
To: djgpp AT delorie DOT com
Subject: exit() from main()
Message-ID: <Pine.OSF.3.91.960916141609.18881A-100000@bud.cc.swin.edu.au>
MIME-Version: 1.0

>> There's nothing wrong with using exit to terminate a program, even
>> in main.
>
>Well, sure there's nothing wrong with it...  But it performs frivolou
>fflush's and other things when all you want to do is end the program
>there.  Why call another function when all you want is to end the pro
>;)

for the following C++ program
-- example.cpp
#include <iostream.h>
#include <stdlib.h>

class foo
{
public:
	foo()
	{
		cout<<"Important init. code"<<endl;
	}

	~foo()
	{
		cout<<"Important end code"<<endl;
	}
};

int main()
{
	foo bar;
	cout<<"Running..."<<endl;
	return 0;
}

--end example.cpp

the results are:
Important init. code
Running...
Important end code

but if main in the above program is changed to

int main()
{
	foo bar;
	cout<<"Running..."<<endl;
	exit(0);
	return 0;//Note no warning on this line in BC4.5
}

the results are
Important init. code
Running...

And the important end code is not called!
There sure is something wrong with this.  This code could be needed
to release hooked interrupts or other hardware (such as the video) which 
could cause BIG problems after the program finishes.

If it is only memory that is not freed (or deleted) then it should get 
cleaned 
up but it sure is nicer if the correct code is executed.

Newbs

--

My .sig is in the FAQ

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019