When Does Cout Flush

When does cout flush?

There is no strict rule by the standard - only that endl WILL flush, but the implementation may flush at any time it "likes".

And of course, the sum of all digits in under 400K is 6 * 400K = 2.4MB, and that's very unlikely to fit in the buffer, and the loop is fast enough to run that you won't notice if it takes a while between each output. Try something like this:

 for(int i = 0; i < 100; i++)
{
cout<<i<<"\n";
Sleep(1000);
}

(If you are using a Unix based OS, use sleep(1) instead - or add a loop that takes some time, etc)

Edit: It should be noted that this is not guaranteed to show any difference. I know that on my Linux machine, if you don't have a flush in this particular type of scenario, it doesn't output anything - however, some systems may do "flush on \n" or something similar.

Why / when is cout buffer automatically flushed?

You are correct that std::endl, or the use of std::cin, causes a flush to occur, and the contents to be immediately output. std::endl is the equivalent of std::cout.put('\n'); std::cout.flush();, where as std::cin and std::cerr are tie()d to std::cout and therefore any operation to either of those executes std::cout.flush() for you.

However, in the second case, std::cout still has an underlying output sequence (an std::streambuf), which is a specified size. Once this buffer is full, the stream is flushed automatically, and you get the output to the console. Filling up this buffer is what you are seeing with the delay time you mentioned.

Why does `\n` flush std::cout?

Yes, endl will cause a flush, but that doesn't mean the buffer can't decide to flush itself for other reasons. See this reference, particularly this:

In many implementations, standard output is line-buffered, and writing '\n' causes a flush anyway

c++ force std::cout flush (print to screen)

Just insert std::flush:

std::cout << "Beginning computations..." << std::flush;

Also note that inserting std::endl will also flush after writing a newline.

Is it possible to make std::cout flush automatically before program termination in various failure cases (e.g., exception)

When program terminates due to uncaught exception, std::terminate will be called. This calls the terminate handler that has been registered using std::set_terminate. Within this terminate handler, you can flush the standard output streams.

std::terminate is also called in some other cases such as throwing exception out of noexcept function, destruction of joinable std::thread, direct call from user code etc.

This does not help if the program is terminated by the operating system. That can only be solved by disabling buffering. You could try to flush in a signal handler as a desperate measure, but that is not guaranteed to succeed, nor to have desired behaviour, because flushing is not async safe operation.

Flush cout manually only

Use std::cout << '\n' instead of std::endl. This avoids the flush after every line. std::endl will always flush, since that is its purpose. There's no option to disable that behavior. However, there's no requirement to use std::endl at all. Ultimately, you can't avoid all flushing as the buffer for std::cout is finite, so eventually, the output will be flushed regardless if you use std::endl or '\n'.

If you want to increase the buffer size for standard output, you could try increase buffer for cout.

How does std::flush work?

Since it wasn't answered what std::flush happens to be, here is some detail on what it actually is. std::flush is a manipulator, i.e., a function with a specific signature. To start off simple, you can think of std::flush of having the signature

std::ostream& std::flush(std::ostream&);

The reality is a bit more complex, though (if you are interested, it is explained below as well).

The stream class overload output operators taking operators of this form, i.e., there is a member function taking a manipulator as argument. The output operator calls the manipulator with the object itself:

std::ostream& std::ostream::operator<< (std::ostream& (*manip)(std::ostream&)) {
(*manip)(*this);
return *this;
}

That is, when you "output" std::flush with to an std::ostream, it just calls the corresponding function, i.e., the following two statements are equivalent:

std::cout << std::flush;
std::flush(std::cout);

Now, std::flush() itself is fairly simple: All it does is to call std::ostream::flush(), i.e., you can envision its implementation to look something like this:

std::ostream& std::flush(std::ostream& out) {
out.flush();
return out;
}

The std::ostream::flush() function technically calls std::streambuf::pubsync() on the stream buffer (if any) which is associated with the stream: The stream buffer is responsible for buffering characters and sending characters to the external destination when the used buffer would overflow or when the internal representation should be synced with the external destination, i.e., when the data is to be flushed. On a sequential stream syncing with the external destination just means that any buffered characters are immediately sent. That is, using std::flush causes the stream buffer to flush its output buffer. For example, when data is written to a console flushing causes the characters to appear at this point on the console.

This may raise the question: Why aren't characters immediately written? The simple answer is that writing characters is generally fairly slow. However, the amount of time it takes to write a reasonable amount of characters is essentially identical to writing just one where. The amount of characters depends on many characteristics of the operating system, file systems, etc. but often up to something like 4k characters are written in about the same time as just one character. Thus, buffering characters up before sending them using a buffer depending on the details of the external destination can be a huge performance improvement.

The above should answer two of your three questions. The remaining question is: When would you flush a stream? The answer is: When the characters should be written to the external destination! This may be at the end of writing a file (closing a file implicitly flushes the buffer, though) or immediately before asking for user input (note that std::cout is automatically flushed when reading from std::cin as std::cout is std::istream::tie()'d to std::cin). Although there may be a few occasions where you explicitly want to flush a stream, I find them to be fairly rare.

Finally, I promised to give a full picture of what std::flush actually is: The streams are class templates capable of dealing with different character types (in practice they work with char and wchar_t; making them work with another characters is quite involved although doable if you are really determined). To be able to use std::flush with all instantiations of streams, it happens to be a function template with a signature like this:

template <typename cT, typename Traits>
std::basic_ostream<cT, Traits>& std::flush(std::basic_ostream<cT, Traits>&);

When using std::flush immediately with an instantiation of std::basic_ostream it doesn't really matter: The compiler deduces the template arguments automatically. However, in cases where this function isn't mentioned together with something facilitating the template argument deduction, the compiler will fail to deduce the template arguments.

When does the buffer flush

You probably want to read about std::unitbuf and std::nounitbuf. Here's a decent reference.

It might be helpful to play with some simple examples. For instance, the following code will not print Hello world ever, because there won't be automatic flushing of the output stream after any output operation:

EXAMPLE 1

#include<iostream>

int main()
{
// ----- Don't want automatic flushing ------
std::cout << std::nounitbuf;
std::cout << "Hello world";

while (1)
{
;
}
}

And this code example will indeed print Hello world, because I specified that I want the automatic flushing to be enabled:

EXAMPLE 2

#include<iostream>

int main()
{
// ----- I do want automatic flushing ------
std::cout << std::unitbuf;
std::cout << "Hello world";

while (1)
{
;
}
}

I know that the infiite while loop looks a bit silly, but it's the easiest and most portable way of pausing the code that I can think of right now (I'm sure there are better ways, though!). This way I don't get flushing because of (1) code termination, (2) destructors being called and (3) so on. It's all down to the properties of std::cout.



Related Topics



Leave a reply



Submit