Path: utzoo!attcan!uunet!cs.utexas.edu!rice!uw-beaver!sumax!thebes!polari!6sigma2 From: 6sigma2@polari.UUCP (Brian Matthews) Newsgroups: comp.lang.c Subject: Re: return in main is *not* equivalent to exit() (well, maybe) Message-ID: <2174@polari.UUCP> Date: 6 Jul 90 17:12:21 GMT References: <25247@mimsy.umd.edu> <4238@jato.Jpl.Nasa.Gov> <833@mwtech.UUCP> Organization: Seattle Online Public Unix (206) 328-4944 Lines: 32 In article <833@mwtech.UUCP> martin@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 | 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@6sceng.UUCP