Path: utzoo!utgpu!news-server.csri.toronto.edu!torsqnt!hybrid!robohack!druid!darcy From: darcy@druid.uucp (D'Arcy J.M. Cain) Newsgroups: comp.lang.c Subject: Re: return in main is *not* equivalent to exit() (was Re: main() arguments) Message-ID: <1990Jul5.133131.9220@druid.uucp> Date: 5 Jul 90 13:31:31 GMT References: <78627@srcsip.UUCP> <78633@srcsip.UUCP> <25247@mimsy.umd.edu> <4238@jato.Jpl.Nasa.Gov> <833@mwtech.UUCP> Reply-To: darcy@druid.UUCP (D'Arcy J.M. Cain) Organization: D'Arcy Cain Consulting, West Hill, Ontario Lines: 34 In article <833@mwtech.UUCP> martin@mwtech.UUCP (Martin Weitzel) writes: >Nearly right! 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 ... of course, all of you readers out there >in net-world allready know what the problem is, don't you :-) You didn't say but I assume that the problem was garbage output at the end of the program? Other ways to fix this: declare buffer as static flush buffers before returning drop the setbuf call The same sort of thing could happen if you malloc space for the buffer and then free it when you are finished doing output. -- D'Arcy J.M. Cain (darcy@druid) | Government: D'Arcy Cain Consulting | Organized crime with an attitude West Hill, Ontario, Canada | (416) 281-6094 |