Topic: Is cout buffered?


Author: smeyers@aristeia.com (Scott Meyers)
Date: 1999/09/18
Raw View
Is cout buffered?  In CD-1, it was clear that it was not:

  From 27.3.1 of CD-1:

  3 The object cout controls output to an unbuffered stream buffer associ-
    ated with the object stdout, declared in <cstdio>.

CD-2 and the standard use wording which makes no statement about buffering:

  From 27.3.1 of CD-2:

  3 The object cout controls output to a stream buffer associated with the
    object stdout, declared in <cstdio>.

Darin Adler consulted the C89 standard (which I don't have), and he sent me
this quote:

  When opened, the standard error stream is not fully buffered; the
  standard input and standard output streams are fully buffered if and only
  if the stream can be determined not to refer to an interactive device.

Questions:
  - Is it true that the default buffering state of cout is the same as the
    default buffering state of C's stdout?
  - If so, is the above quote an accurate summary of stdout's default
    buffering state?

Thanks,

Scott

--
Scott Meyers, Ph.D.                  smeyers@aristeia.com
Software Development Consultant      http://www.aristeia.com/
Visit http://meyerscd.awl.com/ to demo the Effective C++ CD
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.com>
Date: 1999/09/20
Raw View
Scott Meyers wrote:
>
> Darin Adler consulted the C89 standard (which I don't have), and he sent
> me this quote:
>
>   When opened, the standard error stream is not fully buffered; the
>   standard input and standard output streams are fully buffered if and
>   only if the stream can be determined not to refer to an interactive
>   device.
>
> Questions:
>   - Is it true that the default buffering state of cout is the same as
>     the default buffering state of C's stdout?
>   - If so, is the above quote an accurate summary of stdout's default
>     buffering state?

I believe the C++ standard doesn't guarantee this. It does say, in 27.3
para 1:

   The header <iostream> declares objects that associate objects with the
   standard C streams provided for by the functions declared in <cstdio>.

Thus, cin, cout and cerr pass operations on to stdin, stdout and stderr.
But this doesn't preclude their adding buffering, or flushing after every
operation.

However, table 83 defines the "unitbuf" flag as "flushes output after each
output operation." 27.4.4.1 para 3 says that one of the postconditions for
basic_ostream::init is that "unitbuf" must not be set. Then, 27.3.1 paras
4-6 state that cerr and clog are associated with stderr, but that cerr
deviates by having its "unitbuf" flag set.

This would suggest that cin, cout and clog shouldn't have this bit set by
default, and should therefore not disable any buffering provided by the
underlying C file stream. It doesn't seem to promise not to add buffering,
though, when for instance cout goes to the screen. I think the only
absolute guarantee is that cerr flushes after every operation.

Fortunately, you can set or clear "unitbuf".

--

Ciao,                       Paul D. DeRocco
Paul                        mailto:pderocco@ix.netcom.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]