25

I have some C++ code that uses cout statements for debug purposes and for some reason I can't get all the data to print unless I do a std::cout.flush(); at the end.

I don't quite understand why this flush operation is needed.

Anyone have any insight?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Dixon Steel
  • 1,021
  • 4
  • 12
  • 27

7 Answers7

22

To add to the other answers: your debugging statements should instead go to cerr, because:

  • it writes to the standard error, which means that, running the application, you can easily separate the "normal" program output from the errors/debug information via redirection;
  • most importantly, cerr by default is unbuffered, which means that, after each output operation, it will automatically flush itself, and in general this is desirable for errors and debug output.

(source: C++ standard, §27.3.1 ¶4-5, §27.4.2.1.2 table 83)

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • Thanks, I'll remember this for future reference, I normally don't care since I'll remove my cout statements but this project has been one royal pain. I hate third party code. – Dixon Steel Jun 02 '11 at 13:03
13

Are you using std::endl to terminate your lines. This should be the usual practice, until performance issues require otherwise, but for some reason, I see a lot of code which uses '\n' instead.

Otherwise, you can always do:

std::cout.setf( std::ios_base::unitbuf );

as one of the first things in main. This will cause a flush at the end of every <<, which is more than you need, but for diagnostic output to the console, is probably quite acceptable.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
4

Is the data that isn't automatically getting flushed lacking a \n at the end? By default, standard out doesn't get delivered until a carriage return is seen.

Sheena
  • 15,590
  • 14
  • 75
  • 113
mah
  • 39,056
  • 9
  • 76
  • 93
  • If I send an endl it will print as well, but I thought in C++ that is the same as a flush operation? – Dixon Steel Jun 02 '11 at 12:45
  • The carriage return is used as a "flush point", if you will. Keep in mind that C shares the same underlying I/O as C++ and C lacks endl, however the \n still does the trick. std::endl may in fact force the flush; I'd need to examine its implementation (or hear from someone that has) to be sure. – mah Jun 02 '11 at 12:49
  • 1
    This is wrong. Standard out is *not* line buffered. Where buffering is involved, `iostream` works considerably differently than `FILE`. `std::cout` will be flushed by an explicit call to `flush` (including that triggered by `std::endl` or by a read on `std::cin`), or at other unspecified times decided by the implementation. There is no line buffering in `iostream`. – James Kanze Jun 02 '11 at 13:03
  • std::endl _is_ a flush operation, not just a newline. '\n' will not flush, std::endl will flush an iostream. – berkus Nov 22 '13 at 21:51
  • OMG! The ones who developing the MinGW version of gdb do not know, that nobody sets the carriage return. Just horrible! My code worked just perfectly on GNU/Linux, but since I run it for Windows®, the prints got messed *only in the gdb, not in plain terminal*. And this became fixed only when I added the `\r` sign. I just have no comments. @Mah, by the way, as I know, the stdout should be flushed not on a «carriage return», but on a «newline». – Hi-Angel Sep 16 '14 at 05:14
1

"When you send output to a stream, it does not necessarily get printed immediately. Rather, it may wait in a buffer until some unspecified event, e.g. buffer full enough, reading from input, or exit from program. The details may vary."

http://www.cs.hmc.edu/~geoff/classes/hmc.cs070.200109/notes/io.html

Christian Neverdal
  • 5,655
  • 6
  • 38
  • 93
1

It is the right behavior. You probably use std::endl that add \n and flush the buffer. http://www.cplusplus.com/reference/ostream/endl/

You need to flush the stream, if you want to see the output.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41
1

In C++ you can use endl formatter with cout operator rather then flush.

Sheena
  • 15,590
  • 14
  • 75
  • 113
Stuti
  • 1,620
  • 1
  • 16
  • 33
0

The answer of std::endl is only valid if you want a return. Not sure how you would do this if you wanted to flush a command prompt out.

Sheena
  • 15,590
  • 14
  • 75
  • 113
mike
  • 1
  • this post (https://softwareengineering.stackexchange.com/questions/386269/why-do-you-want-to-avoid-flushing-stdout) seemed to have the solution – MoralCode Mar 10 '22 at 21:09