Stream vs Buffer

S

Sandy

Hi,
In one of my interview i was asked the difference between Stream and Buffer.
can anybody explain the difference.

Thanks
 
M

Maxim Yegorushkin

Hi,
In one of my interview i was asked the difference between Stream and
Buffer.
can anybody explain the difference.

Standard streams are just mere formatters. They use streambufffers to do
actual IO.
 
S

Steven T. Hatton

Maxim said:
Standard streams are just mere formatters. They use streambufffers to do
actual IO.
As I understand things (and that may not be well), the default iostream has
a buffer capacity of 1, and therefore isn't really a buffer at all.
Perhaps Dietmar can clear this up?
 
S

Steven T. Hatton

Steven said:
As I understand things (and that may not be well), the default iostream
has a buffer capacity of 1, and therefore isn't really a buffer at all.
Perhaps Dietmar can clear this up?


Whoops! I meant to say 'isn't really buffered at all'.
 
M

Maxim Yegorushkin

As I understand things (and that may not be well), the default iostream
has a buffer capacity of 1, and therefore isn't really a buffer at all.

I'm interested why would you care?
 
D

Dietmar Kuehl

Steven said:
As I understand things (and that may not be well), the default iostream
has a buffer capacity of 1, and therefore isn't really a buffer at all.
Perhaps Dietmar can clear this up?

What is a "default iostream"? There are eight standard stream objects
(cin, cout, cerr, clog and their wide character counterparts). The
standard does not state whether these are buffered or not. Essentially,
whether they are buffered on stream buffer level normally depends on
the implementation strategy with respect to the standard C stream
(stdin, stdout, and stderr): by default, the C and C++ streams are
in some form synchronized, i.e. you can mix input and output of the
C and C++ streams and the character sequences are still in the order
the output was done. To achieve this, the C++ stream buffers are
either unbuffered and use the standard interface of the C streams
internally or the C++ stream buffers somehow share their buffer with
the corresponding C streams. The latter generally only works if the
implementation controls both the C and the C++ implementation. In
any case, on the C level the streams are typically always buffered
unless the C stream is setup explicity be unbuffered.

For all other IOStreams in the standard C++ library, e.g. file and
string streams, it is safe to assume that they are buffered. Whether
user-defined streams are buffered or not depends on the implementation
of the corresponding stream buffer but obviously the C++ standard does
not make any statement here. That is, in summary most stream buffers
are likely to be buffered but the standard does not guarantee anything
in this area. Of course, I'd recommend that all stream buffers should
do actual buffering because this can be used by many algorithms in the
standard C++ library: these can be specialized to cope with stream
buffers specially and e.g. bypass the stream buffer interface in many
interesting situations.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top