I/O implementation in C and C++

Michael Meissner meissner at xyzzy.UUCP
Fri Jun 3 22:00:55 AEST 1988


In article <462 at polari.UUCP> rlb at polari.UUCP (rlb) writes:
| 
| 
| I find it interesting that both K&R (C) and Stroustrup (C++) give
| I/O package implementation examples in which buffer allocation takes
| place at the first I/O, rather than in the "open".  The obvious disadvantage
| to this is error detection; the programmer is surprised to discover that
| although the "open" succeeded, the first I/O fails because of lack of
| memory (and of course it is usually easier to isolate the open and test
| its status than to do the same for the first I/O).  The only advantage I
| can think of is that if an "open" is performed but no I/O is requested,
| you've saved a bit of memory.  Are there other advantages?
| -Ron Burk

While I'm firmly in the camp about open allocating the buffer, I will
concede that doing the allocation at the first I/O has another advantage:
if you use setbuf/setvbuf/setlinebuf/setbuffer on the file, you don't
have to do an allocate/free combination to first allocate the default
buffer, and switch to the user's buffer.
-- 
Michael Meissner, Data General.

Uucp:	...!mcnc!rti!xyzzy!meissner
Arpa:	meissner at dg-rtp.DG.COM   (or) meissner%dg-rtp.DG.COM at relay.cs.net



More information about the Comp.lang.c mailing list