Does NEWLINE always flush stdio buffer?

Peter da Silva peter at ficc.uu.net
Mon Jul 3 23:27:06 AEST 1989


In article <10487 at smoke.BRL.MIL>, gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
> The proposed ANSI Standard for C suggests (sort of a soft requirement)
> what are essentially the current UNIX System V STDIO rules for line-buffered
> streams: not only should the output buffer be flushed when a new-line is
> encountered or the buffer fills up, but also ALL line-buffered output streams
> should be flushed whenever input is requested from an unbuffered stream or
> when line-buffered input has to refill its buffer.  The cost of implementing
> this can be substantial!

No, it is not. In fact, if (popping down to the implementations I'm familiar
with) _filbuf(stdin) calls _flsbuf(stdout) you get all the advantages of
line-buffered output, obviate the need for most explicit fflush() calls, and
have almost no overhead. This behaviour was first implemented (to my
knowledge) in the Berkeley stdio library, and is (I believe) what the dpANS
is referring to.

In fact, if you implement smart flushing, you can abandon the overhead of
line buffering completely, without breaking any working programs.
-- 
Peter da Silva, Xenix Support, Ferranti International Controls Corporation.

Business: uunet.uu.net!ficc!peter, peter at ficc.uu.net, +1 713 274 5180.
Personal: ...!texbell!sugar!peter, peter at sugar.hackercorp.com.



More information about the Comp.lang.c mailing list