#Understanding Buffers in the context of std::cout

11 messages · Page 1 of 1 (latest)

signal crown
#

I've learned that std::cout is buffered, but I'm not quite sure what that precisely means.

According to what I understand, before the outputs provided into std::cout are displayed on the standard output device—in this case, the console—they are first stored in a buffer, which functions as a sort of intermediary storage between the data being generated and its processing.

This guarantees a degree of optimization (which might not be noticeable on small applications) because storing data into buffers is fast while displaying it is relatively sluggish.

1- Is this introductory explanation correct?

I also have some questions related to the functionality of this process. I did research but so far I haven't found layman explanations for them (and I'm not sure if they can be given):

2- Where is this buffer located, exactly?
3- How does std::cout interact with it?
4- The buffer used for std::cout is the same for std::cin?

I think my question is a bit too broad, but I would still appreciate any explanation you can give me! And if that works best, I may ask for each explanation (or a combination of them) in separate posts or in the text help channel. Also, If I'm getting way too ahead of myself, please say so.

/It's my first post and I will correct it if there's something wrong (like the tags). Sorry for any inconvenience.

teal ridgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

crystal palm
#

1 is not wrong, but missing an important detail. The way it's described buffering would be useless. You'll have to actually write to the console eventually and buffering as described would mean it takes [buffering + printing] time instead of [printing] which is worse. The trick is that you hope more things will be printed so you end up with [buffering + buffering + buffering + buffering + buffering + printing] which is faster than [printing + printing + printing + printing + printing].

#

2 is implementation-defined. Some STL implementer made a buffer somewhere, probably inside std::ios_base.

#

3 It puts the data to be printed into a buffer which occasionally gets actually printed out.

#

That might be implicit, because std::cout inherits from classes that do the grunt work.

#

4 Can't do that because you can print things while there still is input data in the buffer. Reusing that buffer seems like a nightmare to keep correct in all the states.

signal crown
# crystal palm 1 is not wrong, but missing an important detail. The way it's described bufferin...

Regarding point 1, let me get this straight:

It is less time-consuming to perform one print operation with x*n amount of data (case [buffering + buffering... + printing]) (case 1) than to perform x print operations with n amount of data each (case [printing + printing...]) (case 2). And that Is because the buffering procedures ultimately take less time than the overall overhead incurred by all of the printing processes we would have in case 2.

Is that it?

crystal palm
#

Sounds right. Writing into a buffer is simply setting some bytes. Printing means interrupts, giving control to the operating system, something something graphics card driver.... it's a lot more than just setting some bytes.

signal crown
#

I see, thank you very much for your help!

#

!solved