Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ flushing the buffer

Tags:

c++

buffer

cout

I know there are many buffer questions on here but I can't seem find a clear answer on this.

std::cout << "write to screen" << std::endl;

I know this code will write to the screen and flush the buffer because of the "endl", but if I wrote this:

std::cout << "write to screen";

Wouldn't the buffer be flushed regardless since the text has been outputted to the screen?

like image 885
Memnite Avatar asked Dec 31 '25 21:12

Memnite


2 Answers

Wouldn't the buffer be flushed regardless since the text has been outputted to the screen?

Assuming that you have seen the text outputted to the screen, then yes, the buffer has been flushed.

I believe the confusion is regarding this line:

std::cout << "write to screen";

The absence of std::endl doesn't mean "don't flush the buffer". It simply means "I'm not saying when to flush the buffer".

like image 136
Drew Dormann Avatar answered Jan 03 '26 11:01

Drew Dormann


There are multiple ways to ensure your std::ostream is flushed:

  1. Manually with std::endl, std::flush, or a direct call to ostream::flush().
  2. Depending on a later used input stream being bound to your ostream: std::basic_ios::tie().
  3. Depending on the tie to C streams: std::ios_base::sync_with_stdio

    This means that anything which would flush the corresponding C stream will also flush the C++ stream, like a call to fflush(), or the (maybe automatically) selected buffering strategy.
    Like line-buffering.

    From C11 draft:

    7.21.3 Files

    3 When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via the setbuf and setvbuf functions.
    7 At program startup, three text streams are predefined and need not be opened explicitly — standard input (for reading conventional input), standard output (for writing conventional output), and standard error (for writing diagnostic output). As initially opened, the standard error stream is not fully buffered; the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device.

  4. Waiting on the internal buffer overflowing.

Now, as a general guideline: Don't manually flush your streams, doing so can significantly degrade performance. Unless, of course, it's neccessary for correctness.

like image 27
Deduplicator Avatar answered Jan 03 '26 11:01

Deduplicator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!