tags:

views:

44

answers:

2

hello everyon, can somebody please explain, what exactly does it mean? I'm trying to understand what is the difference between clog and cerr, difference is only in buffering thanks in advance

+2  A: 

When you write to clog then you actually write to an in-memory store of characters. When this store becomes full, it then writes to the actual stream. When the stream is closed, (which would happen on the program ending) then any remaining data is written to the stream (manual flushing can also happen).

With cerr, this does not happen.

As a rule, since writing to memory is faster than writing to most streams, the effect of buffering is an overall improvement in performance. This isn't always the case (a classic example is where levels of indirection lead to a buffer writing to a buffer writing a buffer), but it is often enough for this to be a reasonable default. A consequence though is that there can be a delay between the stream object being written to, and the actual stream being written to, which is inappropriate in some cases.

It's reasonable to imagine that something writing to cerr may need prompt action, but something writing to clog will not, hence the difference.

Jon Hanna
One could look at buffer -> buffer -> buffer writing as a logical error in the code and thus not really relevant as bugs are generally not faster than correct code.
Martin York
@Martin, if those buffers are all blackboxed away from each other, then its suboptimal, but not incorrect.
Jon Hanna
+1  A: 

The difference between clog and cerr is that clog is fully buffered, whereas output to cerr is written to the external device after each formatting. With a fully buffered stream, output to the actual external device is written only when the buffer is full. Thus clog is more efficient for redirecting output to a file, while cerr is mainly useful for terminal I/O. Writing to the external device after every formatting, to the terminal in the case of cerr, serves the purpose of synchronizing output to and input from the terminal. Also, the predefined streams are synchronized with their associated C standard files.

Sachin Shanbhag