A
A
artemphrog2020-12-15 11:32:12
C++ / C#
artemphrog, 2020-12-15 11:32:12

What is an I/O buffer?

Explain in simple terms what an input or output buffer is, and why it should be cleared, and in which case it should be cleared. Nothing is clear on google.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-12-15
@artemphrog

For input/output to the console or file, the program makes read or write system calls. Without going into details, I will say that they are expensive. If you are reading one character at a time from std::cin, then it will be terribly slow.
What happens in practice: one system call occurs, cinreads into its internal buffer at once, say, 4096 bytes, and then gives you from the buffer one character at a time, which you request from it. It is easy to see that this will work (in my example) somewhere 4096 times faster.
Why clean. Until you do flush, what you put out to the stream is in the buffer. coutwaiting for you to send him more bytes to bring everything out later in a big piece. If you want the output to happen right now, then you need flush.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question