132

I am learning C++ and I found something that I can't understand:

Output buffers can be explicitly flushed to force the buffer to be written. By default, reading cin flushes cout; cout is also flushed when the program ends normally.

So flushing the buffer (for example an output buffer): does this clear the buffer by deleting everything in it or does it clear the buffer by outputting everything in it? Or does flushing the buffer mean something completely different?

Null
  • 1,950
  • 9
  • 30
  • 33
Mohamed Ahmed Nabil
  • 3,949
  • 8
  • 36
  • 49

3 Answers3

170

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.

Chin
  • 19,717
  • 37
  • 107
  • 164
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 1
    Thanks. one more thing. Reading cin flushes cout. Does this "reading cin" mean when the user inputs something or when the user is prompted to enter something? – Mohamed Ahmed Nabil Feb 23 '13 at 16:50
  • 5
    Reading cin happens when you use the stream operator to read from cin. Typically you want to flush cout when you read because otherwise the input may appear before the prompt. – David Heffernan Feb 23 '13 at 16:54
  • 1
    @DavidHeffernan As far as I know, you never need to flush cout before cin, because cin and cout are tied (Stroustrup, The C++ Programming Language, [io.tie]). – sturmer Oct 16 '13 at 07:29
30

You've quoted the answer:

Output buffers can be explicitly flushed to force the buffer to be written.

That is, you may need to "flush" the output to cause it to be written to the underlying stream (which may be a file, or in the examples listed, a terminal).

Generally, stdout/cout is line-buffered: the output doesn't get sent to the OS until you write a newline or explicitly flush the buffer. The advantage is that something like std::cout << "Mouse moved (" << p.x << ", " << p.y << ")" << endl causes only one write to the underlying "file" instead of six, which is much better for performance. The disadvantage is that a code like:

for (int i = 0; i < 5; i++) {
    std::cout << ".";
    sleep(1); // or something similar
}

std::cout << "\n";

will output ..... at once (for exact sleep implementation, see this question). In such cases, you will want an additional << std::flush to ensure that the output gets displayed.

Reading cin flushes cout so you don't need an explicit flush to do this:

std::string colour;
std::cout << "Enter your favourite colour: ";
std::cin >> colour;
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
tc.
  • 33,468
  • 5
  • 78
  • 96
  • Doing this for (int i=0; i<5; i++) { std::cout << "."; sleep(1); } std::cout << std::endl; doesnt print ..... at once. It prints them with 1 millisecond in between. You will notice it more when you use sleep(1000) – Mohamed Ahmed Nabil Feb 23 '13 at 17:00
  • @MohamedAhmedNabil You appear to be confusing `sleep()` (POSIX) with `Sleep()` (Windows) – tc. Feb 23 '13 at 17:03
  • can you explain the difference? – Mohamed Ahmed Nabil Feb 23 '13 at 17:05
  • This is a little misleading, as endl explicitly writes a new line and flushes the buffer. Line buffered would only require it to write the new line. – Alex Chamberlain Feb 23 '13 at 17:45
  • 1
    Old answer, but only comment is less about the content and more about the example. You qualify `cout` with a namespace (i.e., `std::cout`) but did not do so for the `endl`, which should also require that qualification. – vol7ron Feb 03 '16 at 16:16
  • 1
    I like your example. But I thought endl flushes the buffer, but in your example, \n flushes the buffer. I am confused. – Naz Jan 03 '17 at 23:19
  • 2
    @Naz \n does not flush the buffer; the buffer is only flushed at the end of the program in his example (the buffer is always automatically flushed at the end of C++ programs). \n was likely just used for formatting. Also, you are right that std::endl flushes the buffer (so does std::flush, but that's self-explanatory). – liamnickell Feb 14 '17 at 15:17
6

Clear the buffer by outputting everything.

Alex Chamberlain
  • 4,147
  • 2
  • 22
  • 49