26

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?

1

7 Answers 7

23

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)

Sign up to request clarification or add additional context in comments.

1 Comment

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

4 Comments

Yes, I do as a habit, but some of the code I'm integrating does not and it's rather large for me to "correct" it..
@Dixon Steel I know the feeling. That's why I offered the second suggestion, using unitbuf.
Why should std::endl be usual practice?
@9769953 Because it simplifies debugging. (Obviously, it all the program is doing is outputting large amounts of text, you'll want to optimize by using \n. But that should be thought of as an optimization, not as default behavior when performance doesn't matter.)
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.

5 Comments

If I send an endl it will print as well, but I thought in C++ that is the same as a flush operation?
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.
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.
std::endl is a flush operation, not just a newline. '\n' will not flush, std::endl will flush an iostream.
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».
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

1 Comment

So does this mean output can be unpredictable and I don't really have an error or deadlock on the stream?
1

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

Comments

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.

Comments

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.

1 Comment

this post (softwareengineering.stackexchange.com/questions/386269/…) seemed to have the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.