I’m reading accelerated c++ and the author writes:
Flushing the output buffers at opportune moments is an important habit when you are writing programs that might take a long time to run. Otherwise, some of the program’s output might languish in the systems buffers for a long time between when your program writes it and when you see it
Please correct me if i misunderstand any of these concepts:
- Buffer: a block of random access memory that is used to hold input or output temporarily.
- Flushing: freeing up random access memory that had been… eh.. assigned to certain ..umm
There is this explanation I found:
Flushing an output device means that all preceding output operations are required to be completed immediately. This is related to the issue of buffering, which is an optimization technique used by the operating system. Roughly speaking, the operating system reserves (and usually exerts) the right to put the data “on stand by” until it decides that it has an amount of data large enough to justify the cost associated to sending the data to the screen. In some cases, however, we need the guarantee that the output operations performed in our program are completed at a given point in the execution of our program, so we flush the output device.
Continuing from that explanation i read that the three events that cause the system to flush the buffer:
- Buffer becomes full and will automatically flush
- The library might be asked to read from standard input stream *is standard input stream like
std::cin >> name ; - The third occasion is when we explicitly tell it to. How do we explicitly tell it to?
Despite I don’t feel like a fully grasp the following:
- What a output buffer is vs just a buffer and presumable other types of buffers…
- What it means to flush a buffer. Does it simply mean to clear the ram?
- What is the “output device” refereed to in the above explanation
- And finally after all this when are opportune moments to to flush your buffer…ugh that doesn’t sound pleasant.
You explicitly flush a stream with
your_stream.flush();.A buffer is usually a block of memory used to hold data waiting for processing. One typical use is data that’s just been read from a stream, or data waiting to be written to disk. Either way, it’s generally more efficient to read/write large blocks of data at a time, so read/write an entire buffer at a time, but the client code can read/write in whatever amount is convenient (e.g., one character or one line at a time).
That depends. For an input buffer, yes, it typically means just clearing the contents of the buffer, discarding any data that’s been read into the buffer (though it doesn’t usually clear the RAM — it just sets its internal book-keeping to say the buffer is empty).
For an output buffer, flushing the buffer normally means forcing whatever data is in the buffer to be written to the associated stream immediately.
When you’re writing data, it’s whatever device you’re ultimately writing to. That could be a file on the disk, the screen, etc.
One obvious opportune moment is right when you finish writing data for a while, and you’re going to go back to processing (or whatever) that doesn’t produce any output (at least to the same destination) for a while. You don’t want to flush the buffer if you’re likely to produce more data going the same place right afterward — but you also don’t want to leave the data in the buffer when there’s going to be a noticeable delay before you fill the buffer (or whatever) so the data will get written to its destination.