explain buffer and whats difference between cerr and clog
Can u explain the concept of buffer?
@taran9873
Buffer is like a temporary storage.
Consider this analogy for a minute.
Your house generates waste throughout the day - wrappers , fruit peels, paper .
Do you go out to throw every piece of paper to your nearest waste area or do you dump it all in your dustbin and empty the dustbin into the waste area when the dustbin gets full ?
The reason is simple that we do this … it’s easier and more efficient to store it and then make a single trip to empty the waste together.
Similar is the concept with input-output data.
While taking input , we ( the human users ) are extremely slow compared to CPU. So if the CPU takes input directly as we give it , it would have to wait for long CPU cycles. Instead , when we write the input , it is stored in an input buffer and when the buffer gets full , it is emptied out and the data is transferred to CPU. This way , CPU does not have to wait for us and it can go on with its other tasks while work on input when it gets it from the buffer.
Just like input buffer , there is also an output buffer to buffer up the output stream .
Buffering increases efficiency drastically and is much better than reading and processing Input-Output simultaneously as it is generated.
As for cerr and clog,
cerr and clog are both objects of the stderr stream. Following are the differences between them. You can also read about the cout object to get a clearer picture.
Un-buffered standard error stream (cerr)
cerr is the standard error stream which is used to output the errors. This is also an instance of the
ostream class. As cerr is un-buffered therefore it’s used when we need to display the error message instantly. It doesn’t have any buffer to store the error message and display later.
Buffered standard error stream (clog)
This is also an instance of ostream class and used to display errors but unlike cerr the error is first inserted into a buffer and is stored in the buffer until it is not fully filled.
Standard output stream (cout)
cout is the instance of the ostream class. cout is used to produce output on the standard output device which is usually the display screen. The data needed to be displayed on the screen is inserted in the standard output stream (cout) using the insertion operator (<<).
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.