Possible Duplicate:
Why does my cout output not appear immediately?
I have a very heavy method (it checks if a number is a prime – Euler 3), which blocks cout.
How is this possible? This is my code:
int main(int argc, char * argv[]) {
cout << "-----------------------------------------------------------" << endl;
cout << "isPrime(3): " << ((isPrime(3)) ? "true" : "false") << endl;
cout << "isPrime(10): " << (isPrime(10) ? "true" : "false") << endl;
cout << "BLAH";
cout << "BLAH";
cout << "BLAH";
cout << "BLAH";
cout << "BLAH";
cout << "BLAH";
cout << "isPrime(600851475143): " << (isPrime(600851475143.0) ? "true" : "false") << endl; // This one takes very long to complete
cout << "-----------------------------------------------------------";
}
Like this, it outputs:
[Session started at 2013-01-19 13:50:12 +0100.]
-----------------------------------------------------------
isPrime(3): false
isPrime(10): false
and then stops (for a few minutes). (isPrime() is broken, I know!)
If I comment the line with isPrime(600851475143) out, it outpus everything except the output of the commented line of course in less than a second.
How is it possible that a very heavy method call blocks output that should already have been written to cout?
coutwrites to the standard output, which is typically line-buffered. i.e. the buffer is only flushed to the console when it encounters a newline character or anendl, or when you explicitly callcout.flush().