In case of buffered stream it said in a book that it wait until the buffer is full to write back to the monitor. For example:
cout << "hi";
What do they mean by "the buffer is full".
cerr << "hi";
It is said in my book that everything sent to
cerr
is written to the standard error device immediately, what does it mean?char *ch; cin>> ch; // I typed "hello world";
In this example
ch
will be assigned to "hello" and "world" will be ignored does it mean that it still in the buffer and it will affect the results of future statements?
With buffered output there's a region of memory, called a buffer, where the stuff you write out is stored before it is actually written to the output. When you say
cout << "hi"
the string is probably only copied into that buffer and not written out. cout usually waits until that memory has been filled up before it actually starts writing things out.The reason for this is because usually the process of starting to actually write data is slow, and so if you do that for every character you get terrible performance. A buffer is used so that the program only has to do that infrequently and you get much better performance.
It just means that no buffer is used. cerr might still send 'h' and 'i' at the same time since it already has both of them.
This doesn't really have anything to do with buffering. the operator >> for char* is defined to read until it sees whitespace, so it stops at the space between "hello" and "world". But yes, the next time you read you will get "world".
(although if that code isn't just a paraphrase of the actuall code then it has undefined behavior because you're reading the text into an undefined memory location. Instead you should do:
)
You can check out the differences yourself with a small app.
Try out the difference of the two "Start" statments, then change to cerr. The difference you notice is due to buffering.
The for-statement takes about 2 seconds on my rig, you might need to tweak the i < condition on yours.
Your book doesn't seem very helpful.
1) The output streams send their bytes to a
std::streambuf
, which may contain a buffer; thestd::filebuf
(derived fromstreambuf
) used by andstd::ofstream
will generally be buffered. That means that when you output a character, it isn't necessarily output immediately; it will be written to a buffer, and output to the OS only when the buffer is full, or you explicitly request it in some way, generally by callingflush()
on the stream (directly, or indirectly, by usingstd::endl
). This can vary, however; output tostd::cout
is synchronized withstdout
, and most implementations will more or less follow the rules ofstdout
forstd::cout
, changing the buffering strategy if the output is going to an interactive device.At any rate, if you're unsure, and you want to be sure that the output really does leave your program, just add a call to flush.
2) Your book is wrong here.
One of the buffering strategies is
unitbuf
; this is a flag in thestd::ostream
which you can set or reset (std::ios_base::set()
andstd::ios_base::unset()
—std::ios_base
is a base class ofstd::ostream
, so you can call these functions on anstd::ostream
object). Whenunitbuf
is set,std::ostream
adds a call toflush()
to the end of every output function, so when you write:the stream will be flushed after all of the characters in the string are output, provided
unitbuf
is set. On start-up,unitbuf
is set forstd::cerr
; by default, it is not set on any other file. But you are free to set or unset it as you wish. I would recommend against unsetting it onstd::cerr
, but ifstd::cout
is outputting to an interactive device, it makes a lot of sense to set it there.Note that all that is in question here is the buffer in the
streambuf
. Typically, the OS also buffers. All flushing the buffer does is transfer the characters to the OS; this fact means that you cannot useofstream
directly when transactional integrity is required.3) When you input to a string or a character buffer using
>>
, thestd::istream
first skips leading white space, and then inputs up to but not including the next white space. In the formal terms of the standard, it "extracts" the characters from the stream, so that they will not be seen again (unless you seek, if the stream supports it). The next input will pickup where ever the previous left off. Whether the following characters are in a buffer, or still on disk, is really irrelevant.Note that the buffering of input is somewhat complex, in that it occurs at several different levels, and at the OS level, it takes different forms depending on the device. Typically, the OS will buffer a file by sectors, often reading several sectors in advance. The OS will always return as many characters as were demanded, unless it encounters end of file. Most OSs will buffer a keyboard by line: not returning from a read request until a complete line has been entered, and never returning characters beyond the end of the current line in a read request.
In the same manner as
std::ostream
uses astreambuf
for output,std::istream
uses one to get each individual character. In the case ofstd::cin
, it will normally be afilebuf
; when theistream
requests a character, thefilebuf
will return one from its buffer if it has one; if it doesn't, it will attempt to refill the buffer, requesting e.g. 512 (or whatever its buffer size is) characters from the OS. Which will respond according to its buffering policy for the device, as described above.At any rate, if
std::cin
is connected to the keyboard, and you've typed"hello world"
, all of the characters you've typed will be read by the stream eventually. (But if you're using>>
, there'll be a lot of whitespace that you won't see.)streams in C++ are buffer to increase efficiency, that is file and console IO is very slow in comparison to memory operations.
To combat this C++ streams have a buffer (a bank of memory) that contains everything to write to the file or output, when it is full then it flushed to the file. The inverse is true for input, it fetches more when it the buffer is depleted.
This is very import for streams because the following
Would be 4 writes to a file which is inefficient.
However in the case of std::cout, cin, and cerr then these type actually have buffering turned off by default to ensure that it can be used in conjunction with std::printf and std::puts etc...
To re-enable it (which I recommend doing):
But don't use C style console output whilst it is set false or bad things may happen.
Each call to write to the terminal is slow, so to avoid doing slow things often the data is stored in memory until either a certain amount of data has been entered or the buffer is flushed manually with fflush or std::endl. The result of this is sometimes that text might not be written to the terminal at the moment you expect it to.
Since the timing of error messages is more critical than normal output, the performance hit is ignored and the data is not buffered. However, since a string is passed in a single piece of data, it is written in one call (inside a loop somewhere).
It world would still be in the buffer, but it's quite easy to prove this yourself by trying it in a 3 line program. However, your example will fail since you are attempting to write into unallocated memory. You should be taking input into a std::string instead.