 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ganny Guest
|
Posted: Thu Nov 24, 2005 8:10 am Post subject: why is only cerr unitbuffered by default? |
|
|
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! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Fri Nov 25, 2005 6:15 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
"Ganny" <sgganesh (AT) gmail (DOT) com> writes:
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Sat Nov 26, 2005 2:30 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Ganny <sgganesh (AT) gmail (DOT) com> wrote:
| Quote: | 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! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Sat Nov 26, 2005 2:35 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Ganny wrote:
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Sebastian Redl Guest
|
Posted: Sat Nov 26, 2005 2:43 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Ganny wrote:
| Quote: | 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! ]
|
|
| Back to top |
|
 |
Vector Guest
|
Posted: Sat Nov 26, 2005 2:47 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
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! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sat Nov 26, 2005 2:59 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Thomas Maeder wrote:
| Quote: | "Ganny" <sgganesh (AT) gmail (DOT) com> writes:
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! ]
|
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Sun Nov 27, 2005 4:04 am Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Vector wrote:
| Quote: | 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.
| Quote: | 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.
| Quote: | 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! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Nov 28, 2005 3:49 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Alberto Ganesh Barbati wrote:
| Quote: | Vector wrote:
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.
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! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Nov 28, 2005 3:58 pm Post subject: Re: why is only cerr unitbuffered by default? |
|
|
Sebastian Redl wrote:
| Quote: | Ganny wrote:
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.)
|
Flushing only transfers the data to the OS. It doesn't wait
until the data is actually written to the media, unless the OS
request to write data waits (which would be rather unusual in a
modern OS). In practice, the difference in performance should be
very slight, although this depends on the cost of a context
switch on the machine.
Note too that std::endl flushes the stream. So if you are using
std::endl, it is the equivalent of using a line buffered stream,
an option which is not otherwise available with iostreams. In
C, stderr -- and stdout, if it is connected to an interactive
stream, are at most line buffered; they cannot be fully
buffered. The equivalent of this in C++ is to use std::endl,
rather than 'n'. Personally, I would recommend this as general
practice until you fully understand the issues of buffering
(including system buffering), and even as a general default
later.
A quick test on my machine: to write a million records, each
with 3 ints, a double and a 20 character string, takes roughly
45 seconds with full buffering, one minute using endl to
simulate line buffering, and 2 minutes with unit buffering. And
only 45 seconds if request no buffering what so ever -- a quick
check, however, shows that the request is ignored by both g++
and Sun CC, this despite §27.8.1.4/10.
I'm not sure why line buffering isn't available in C++, at least
for output files. It makes far more sense to control this at
the file level than in each output statement.
| Quote: | 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.
|
And when the system crashes, data buffered by the OS is lost;
even the fact that you've flushed it is no guarantee.
During debugging, this is not a problem; it's your program which
is crashing, and not the system. Depending on what your program
does, this *may* be a problem once it is deployed; in my
applications (servers with persistent data), it is usual to
"write" to an ostringstream (or an ostrstream in older
software), and then use a low level system function to do the
synchronized physical write to disk.
--
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! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|