return in main is *not* equivalent to exit() (well, maybe)

Brian Matthews 6sigma2 at polari.UUCP
Sat Jul 7 03:12:21 AEST 1990


In article <833 at mwtech.UUCP> martin at mwtech.UUCP (Martin Weitzel) writes:
|I'll never forget the day when I tracked down what
|seemed to be a really wierd problem in some printer driving code
|to the following (still buggy!) program:
|
|	#include <stdio.h>
|	int main(argc, argv)
|		int argc; char **argv; /* OK so, Chris? :-) */
|	{
|		char buffer[BUFSIZ];
|		setbuf(stdout, buffer);
|		........ /* some code which produced output */
|		return 0;
|	}
|
|Note that the bug could be eliminated by replacing `return 0;'
|with `exit (0);'. Well, after that the cause for this problem
|became obvious to me...

I don't have my copy of the standard here, but I believe the section
on setvbuf (which also applies to setbuf) says something about the
buffer having to exist as long as the stream is open.  You don't
close stdout before the return, so the automatic buffer is destroyed
before the stream is closed.  The program doesn't conform to the
standard, so it's NOT an example of a difference between return
and exit (as far as the standard is concerned).

And yes, this sort of thing can be tough to debug.  Usually when
I see an automatic array or structure, it screams to be looked at
to make sure it can't be used outside of the block or its children.
-- 
Brian L. Matthews	blm at 6sceng.UUCP



More information about the Comp.lang.c mailing list