Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!ucsfcgl!cca.ucsf.edu!wet!epsilon From: epsilon@wet.UUCP (Eric P. Scott) Newsgroups: comp.sources.games.bugs Subject: Re: What setbuf() is and why you should use it Keywords: curses fflush setbuf stdio Message-ID: <295@wet.UUCP> Date: 18 Jul 89 20:25:10 GMT References: <283@wet.UUCP> <2245@auspex.auspex.com> Reply-To: epsilon@wet.UUCP (Eric P. Scott) Organization: Wetware Diversions, San Francisco Lines: 45 In article <2245@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes: >>The fix is very simple: put >> setbuf(stdout, malloc(BUFSIZ)); >> >>early in main(), > >In which case, if you run out of e.g. swap space (or address space, >unless you've got tons of it and haven't consumed it all due to some >other bug), you may drop core. I got a mail message claiming the same thing--on which system does it occur? All the ones I know of simply have malloc() return a NULL pointer, which is valid input to setbu()--it sets the stream unbuffered. >[suggested checking malloc's return value] In any case, if the first thing you do in a program is malloc() and it fails, you're probably in serious trouble anyway. (I normally practice a fairly paranoid programming style, this being one of the exceptions.) Also, NULL should always be cast to a pointer of the desired type when used explicitly. I prefer if (!(x)) to test for this case; the ! operator is true against a NULL pointer type, \even if/ your architecture uses something other than 0 to represent NULL, and false otherwise. >[suggested using a static buffer] I've run across stdio implementations that are documented to only work with buffers obtained from malloc(). (I presume this means they free() on fclose, or somesuch nonsense. There should be a "MYBUF" flag, but since that isn't part of the documented interface, some vendors didn't include it. SunOS users: your stdio is different from all UNIX flavors I know about. See the setbuf(3s) manual page. BSD users will probably want to use the BSD-specific buffering functions as well. It's a lot easier to change a setbuf() to something "better" than to find all the places to fflush if it's not done automatically as a side effect of reading stdin. I.e. plan for fully buffered terminal output. -=EPS=-