Re: "endl and "\n"

From: Minti (mintiSPAMBLOCK_at_yahoo.com)
Date: 07/07/04


Date: 7 Jul 2004 10:59:13 -0700

Karl Heinz Buchegger <kbuchegg@gascad.at> wrote in message news:<40EBB79C.E7B92E7E@gascad.at>...
> dover wrote:
> >
> > For the code, outputfile << *p << endl;
> >
> > Someone suggests: Don't use endl here; it is flushing output stream every
> > time. Use plain '\n'.
> >
> > What's the difference of using "endl" and "\n" above? What's the meaning of
> > flushing output stream? Isn't that what we want?
>
> The point is:
> Most operating systems or most C++ libraries do a sort of buffering.
> If you do an output, the output is not immediatly sent to the
> output device, but instead is buffered until a specific amount of
> buffered output is collected, which is then sent in one chunk to the
> device. The reason why this is done is simply efficiency. To hand some
> data through to the receiving device, the data has to pass various stages
> in either the C++ library and/or the operating system. All of them take
> time and it would be a waste of time to do that for eg. each single
> character.
>
> But sometimes you want exactly that: no buffering, each character is sent
> immediatly to the device as soon as it is available. This is the situation
> where flushing comes into play. It is the request to empty all those buffers
> and sent the data to the device, regardless of buffering strategy.
> Example: If you do debugging the old way by inserting output statements into
> your source code and watching which one you saw last before the crash. Here
> you don't want some buffering to occour and thus you flush the output stream.

Yes, we all know this but what surprises me is that most of textbook
authors pretty much NEVER use "\n", they just keep on using std::endl,
without highlighting the potential loss in efficiency.

-- 
Imanpreet Singh Arora       "In order to paint 6 things are required:
i"kaur" #AT# acm #DOT# org    spirit, rhythm, thought, scenery, pen
and ink"
mv kaur singh


Relevant Pages

  • Re: "endl and " "
    ... Most operating systems or most C++ libraries do a sort of buffering. ... you don't want some buffering to occour and thus you flush the output stream. ...
    (comp.lang.cpp)
  • which OutputStreams are buffered?
    ... There is a java.io.BufferedOutputStream whose purpose is well documented, basically as a good thing to wrap around an unbuffered OutputStream (at least if you want buffering). ... an output stream for writing data to a File ...". ...
    (comp.lang.java.programmer)
  • Re: "endl and " "
    ... Somebody had already given you the answer;-), it is flushing output stream ... Buffering here means that not everything that you write into the ... it and at some point they flush it to the external device. ... With every endl you flush the stream which may be an overkill. ...
    (comp.lang.cpp)