What Does Flushing the Buffer Mean

What does flushing the buffer mean?

Consider writing to a file. This is an expensive operation. If in your code you write one byte at a time, then each write of a byte is going to be very costly. So a common way to improve performance is to store the data that you are writing in a temporary buffer. Only when there is a lot of data is the buffer written to the file. By postponing the writes, and writing a large block in one go, performance is improved.

With this in mind, flushing the buffer is the act of transferring the data from the buffer to the file.

Does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it?

The latter.

What does it mean to flush the input buffer ?

"Flushing the input buffer" refers to the attempt to discard unwanted characters from the input stream so that they do not perturb later input calls.

In your code, it doesn't look like you'll have this problem, so flushing the input buffer should not be an issue for you.

The unwanted input issue typically occurs when you're doing input using scanf. scanf typically leaves the user's newline on the input buffer, but later calls to getchar or fgets (or even scanf) can be badly confused by this.

The problem with flushing the input is that there isn't really a good way of doing it. A popular although not recommended technique is to call fflush(stdin). That looks like it ought to be just the ticket, but the problem is that it's not well-defined and not guaranteed to work (although some programmers have found that it works well enough for them, on some platforms).

See this question and this question (maybe also this one) for much more on this issue.

What is meant by 'flushing the stream'?

Flushing a stream ensures that all data that has been written to that stream is output, including clearing any that may have been buffered.

Some streams are buffered to aid performance, e.g. a stream writing to disk may buffer until the content reaches a block size.

endl and flushing the buffer

Output is generally buffered before it's written to the intended device. That way, when writing to slow to access devices(like files), it doesn't have to access the device after every single character.

Flushing means emptying the buffer and actually writing it to the device.

when is a opportune moment to flush the output buffer and some basic c++

You explicitly flush a stream with your_stream.flush();.

What a output buffer is vs just a buffer and presumable other types of buffers...

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).

What it means to flush a buffer. Does it simply mean to clear the ram?

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.

What is the "output device" refereed to in the above explanation

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.

And finally after all this when are opportune moments to to flush your buffer...ugh that doesn't sound pleasant.

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.

How to know when a buffer flush is needed?

sync()

You seldom need a call to sync(). When you do, you have something that it is crucial must be recorded on disk ASAP. However, sync() will return having scheduled the writing of buffers in kernel memory, not after they've been written, so you won't know that they've actually been written — so it isn't wholly reliable. If you need more control over the writing for your file, look at the O_SYNC, O_DSYNC, O_RSYNC flags for open(). You will probably have to use fcntl() and fileno() to set these flags if you use file streams rather than file descriptors.

Two caveats:

  1. sync() won't write buffers from your process (or any other process) to the kernel buffer pool; it is wholly unrelated to fflush().
  2. sync() affects all data written by all processes on the system — you can become unpopular if your application uses it very often; it subverts the good work the kernel does caching data.

fflush()

The fflush() function ensures that data has been written to the kernel buffer pools from your application's buffer (either for a single file, or for all output files if you use fflush(0) or fflush(NULL)). It doesn't directly affect other processes. Again, you use this when you need to be confident that pending output has been sent to the kernel for onwards transmission. One place where you might use it is before an input operation where you want the prompt to appear, even if it hasn't got a newline at the end. Otherwise, you don't often use it, but you can use it whenever you want to be sure data has been sent to the kernel for writing. If you're debugging and your program crashes, a sprinkling of fflush() statements can ensure that pending output is written before the crash. This can help reveal more accurately where the problem is (but so can using a debugger).

Note that setting unbuffered output (setbuf(stdout, NULL) or setvbuf(stdout, NULL, _IONBF, 0)) means all output occurs 'immediately'. This is not necessarily good for performance. You use it sometimes, but only fairly rarely.

c++ flushing the buffer

Wouldn't the buffer be flushed regardless since the text has been outputted to the screen?

Assuming that you have seen the text outputted to the screen, then yes, the buffer has been flushed.

I believe the confusion is regarding this line:

std::cout << "write to screen";

The absence of std::endl doesn't mean "don't flush the buffer". It simply means "I'm not saying when to flush the buffer".

What is the purpose of flush() in Java streams?

From the docs of the flush method:

Flushes the output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.

The buffering is mainly done to improve the I/O performance. More on this can be read from this article: Tuning Java I/O Performance.

Flushing buffers in C

Flushing the output buffers:

printf("Buffered, will be flushed");
fflush(stdout); // Prints to screen or whatever your standard out is

or

fprintf(fd, "Buffered, will be flushed");
fflush(fd); //Prints to a file

Can be a very helpful technique. Why would you want to flush an output buffer? Usually when I do it, it's because the code is crashing and I'm trying to debug something. The standard buffer will not print everytime you call printf() it waits until it's full then dumps a bunch at once. So if you're trying to check if you're making it to a function call before a crash, it's helpful to printf something like "got here!", and sometimes the buffer hasn't been flushed before the crash happens and you can't tell how far you've really gotten.

Another time that it's helpful, is in multi-process or multi-thread code. Again, the buffer doesn't always flush on a call to a printf(), so if you want to know the true order of execution of multiple processes you should fflush the buffer after every print.

I make a habit to do it, it saves me a lot of headache in debugging. The only downside I can think of to doing so is that printf() is an expensive operation (which is why it doesn't by default flush the buffer).


As far as flushing the input buffer (stdin), you should not do that. Flushing stdin is undefined behavior according to the C11 standard §7.21.5.2 part 2:

If stream points to an output stream ... the fflush function causes any unwritten data for that stream ... to be written to the file; otherwise, the behavior is undefined.

On some systems, Linux being one as you can see in the man page for fflush(), there's a defined behavior but it's system dependent so your code will not be portable.

Now if you're worried about garbage "stuck" in the input buffer you can use fpurge() on that.
See here for more on fflush() and fpurge()



Related Topics



Leave a reply



Submit