Discussion:
why is only cerr unitbuffered by default?
(too old to reply)
Ganny
2005-11-24 08:10:34 UTC
Permalink
why is cerr unitbuffered by default (cin and cout are not unitbuffered
by default).

Thanks!
-Ganesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Thomas Maeder
2005-11-25 18:15:13 UTC
Permalink
Post by Ganny
why is cerr unitbuffered by default (cin and cout are not
unitbuffered by default).
If a process terminates unexpectedly, I am usually thankful for
everything it wrote to std::cerr and std::clog; if they are
unbuffered, nothing will be lost in an unflushed buffer.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Alberto Ganesh Barbati
2005-11-26 14:59:03 UTC
Permalink
Post by Thomas Maeder
Post by Ganny
why is cerr unitbuffered by default (cin and cout are not
unitbuffered by default).
If a process terminates unexpectedly, I am usually thankful for
everything it wrote to std::cerr and std::clog; if they are
unbuffered, nothing will be lost in an unflushed buffer.
cerr is not unbuffered, it's unit-buffered, that means it will be
flushed automatically after every output operation

OTOH, clog is probably buffered (although it's not required nor
guaranteed to be so)

The presence of both of them allows the programmer to choose between two
different approaches: clog for more efficient output (flush must be
explicitly requested) but unsafe (in case of a crash, data in the buffer
might be lost); cerr for safer output (buffer is flushed after every
operation) but less efficient (stream may be flushed more often than
really needed).

For cout the standard gives only one choice: the more efficient one.
That's probably because it's more likely that messages produced during a
critical situation are output on cerr/clog rather than on cout. Anyway,
the programmer can always set cout to unitbuf explicitly if he/she wants.

For cin unitbuf doesn't make sense, because you can't flush an input stream.

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
kanze
2005-11-26 14:35:45 UTC
Permalink
Post by Ganny
why is cerr unitbuffered by default (cin and cout are not
unitbuffered by default).
Because cerr is used for outputting error text, which presumably
has to appear immediately, and should also appear even if the
program aborts immediately after. Typically (hopefully?) too,
there will be very little output on cerr, so the loss of
performance doesn't matter.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Carl Barron
2005-11-26 14:30:46 UTC
Permalink
Post by Ganny
why is cerr unitbuffered by default (cin and cout are not unitbuffered
by default).
Cerr is meant for error messaging, rather than normal output so is
unitbuffered so that the output is presented now incase the stream never
flushes or is destructed. cin,cout are not unit buffered for speed
reasons. Its also how stderr/stdout of C work.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Vector
2005-11-26 14:47:50 UTC
Permalink
I guess u meant unbuffered instead of unitbuffered......

cerr is like cout.It is basically an error stream that is attached to
the console.You could have used cout there but the advantage with cerr
is that it can be re-routed to a logfile and cout only points to the
console screen.
cerr is unbuffered, where the stream is flushed after every output
operation. cout is not required to do this.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Alberto Ganesh Barbati
2005-11-27 04:04:13 UTC
Permalink
Post by Vector
I guess u meant unbuffered instead of unitbuffered......
No, the OP really meant unitbuffered. He is referring to the fact that
cerr has the flag "unitbuf" set.
Post by Vector
cerr is like cout. It is basically an error stream that is attached to
the console.You could have used cout there but the advantage with cerr
is that it can be re-routed to a logfile and cout only points to the
console screen.
This is inaccurate. First of all cerr is unitbuffered, while cout is
not, so they *are* different. Secondly, even cout can be re-routed, by
calling the C function freopen on stdout.
Post by Vector
cerr is unbuffered, where the stream is flushed after every output
operation. cout is not required to do this.
That's the definition of unitbuffered. An unbuffered stream would be one
where each character is written immediately to the "final" destination
(whatever that is) before starting to process the subsequent character.
That is roughly equivalent to a stream with only a 1-char buffer. A
unitbuffered stream is different because an output operation may require
several characters to complete and the stream is flushed as soon as the
operation completes (precisely, in the destructor of the sentry object)
and not after each single character.

Ganesh

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
kanze
2005-11-28 15:49:44 UTC
Permalink
Post by Alberto Ganesh Barbati
Post by Vector
I guess u meant unbuffered instead of unitbuffered......
No, the OP really meant unitbuffered. He is referring to the
fact that cerr has the flag "unitbuf" set.
Post by Vector
cerr is like cout. It is basically an error stream that is
attached to the console.You could have used cout there but
the advantage with cerr is that it can be re-routed to a
logfile and cout only points to the console screen.
This is inaccurate. First of all cerr is unitbuffered, while
cout is not, so they *are* different. Secondly, even cout can
be re-routed, by calling the C function freopen on stdout.
More to the point, where they go depends on the system and
context; I write server software, which runs deconnected from
any terminal, and typically, both cout and cerr are connected to
/dev/null; in cron jobs, they end up being mailed to the user. I
don't know were cout and cerr go in a Windows program (or GUI
programs under Unix), but there generally isn't an associated
terminal there either to which they can be sent.

Secondly, of course, freopen doesn't affect cout and cerr. You
can reroute them by using the non-const basic_ios<>::rdbuf(),
but typically, it is easier to just open a new file.

Most of the time, rerouting is done by the system, before the
program is actually started -- both the Windows shell and all of
the Unix shells use "> file" in the command line to reroute cout
to "file"; the Unix shells I use (ksh and bash) allow "2> file"
to reroute cerr, but I don't know if this is possible in the
Windows shell, and I don't think it is possible in Unix shells
based on the csh. In fact, for programs invoked from the command
line, it is fairly rare for cerr to be redirected, even in
shells where it is possible, where as redirecting cout is rather
common.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Sebastian Redl
2005-11-26 14:43:43 UTC
Permalink
Post by Ganny
why is cerr unitbuffered by default (cin and cout are not unitbuffered
by default).
Efficiency vs security. Flushing the buffer can take some time (esp. on slow
terminals, e.g. SSH connections over a slow network), so cout flushes as
rarely as possible. (cin is an input stream, flushing it ought to have no
effect.)
However, when your program crashes, all buffered data is lost. This is
especially bad for a stream that's supposed to convey error messages. This
is why cerr is unitbuffered by default.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
kanze
2005-11-28 15:58:32 UTC
Permalink
This post might be inappropriate. Click to display it.
Loading...