1

I am trying to print results in 2 nested for cycles using std::cout. However, the results are not printed to the console immediately, but with delay (after both for cycles or the program have been finished).

I do not consider such behavior normal, under Windows printing works OK. The program does not use threads.

Where could be the problem? (Ubuntu 10.10 + NetBeans 6.9).

6
  • How much delay are you talking about? Mar 25, 2011 at 20:17
  • 3
    Are you ever printing endl? That should flush the buffer
    – Cameron
    Mar 25, 2011 at 20:17
  • No code, common printing in two nested for cycles. There is nothing to analyze in code :-))
    – johny
    Mar 25, 2011 at 20:18
  • 1
    As Cameron pointed out, it sounds like you're not flushing the buffer by using std::endl (write new line and flush buffer) or std::flush (just flush buffer). Mar 25, 2011 at 20:19
  • 4
    @johny: We wouldn't have to ask all these questions if you posted the code. "Common printing" is hardly specific enough for us to help, except by guessing
    – Cameron
    Mar 25, 2011 at 20:21

2 Answers 2

9

std::cout is an stream, and it is buffered. You can flush it by several ways:

std::cout.flush();

std::cout << std::flush;

std::cout << std::endl;  // same as:  std::cout << "\n" << std::flush`


johny:

I am flushing the buffer before the cycle using std::endl. The problem arises when printing dot representing % of processed data inside the cycle.

If you flush the buffer before the cycle, that does not affect the output in the cycle. You have to flush in or after the cycle, to see the output.

3
  • I am flushing the buffer before the cycle using std::endl. The problem arises when printing dot representing % of processed data inside the cycle.
    – johny
    Mar 25, 2011 at 20:31
  • @johny: You need to flush inside the cycle, or else cout can delay printing as long as it wants. If you don't want newlines, use std::flush instead of std::endl.
    – aschepler
    Mar 25, 2011 at 20:39
  • @aschepler - I would like to add (while it isn't important for your case) that ::std::cout cannot delay 'as long as it wants'. First, if you read data using ::std::cin a flush will happen. Secondly, it must flush before your program ends if it ends by a normal return from main (even if that return is non-zero). Also, buffer flushes are expensive, don't do them unless you have to (including by using ::std::endl). Mar 25, 2011 at 21:54
1

If you don't flush your output, your output is not guaranteed to be visible outside your program. The fact that it is not printing in your terminal is just a consequence of the default behavior in linux to do line buffering when the output is a tty. If you run your program on linux with its output piped to another thing, like

 ./your_program | cat

Then the default buffer will be MUCH larger, most likely it will be at least 4096 bytes. So nothing will get displayed until the big buffer gets full. but really, the behaviour is OS specific unless you flush std::cout yourself.

To flush std::cout, use :

std::cout << std::flush;

also, using

std::cout << std::endl;

is a shortcut to

std::cout << '\n' << std::flush;

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.