Why is cout outputting immediately?
Asked Answered
L

2

5

cout is a buffered stream. This means that the data will be written to the buffer and will be printed when the stream is flushed, program terminated or when the buffer is completely filled.

I made a small program to test how this works, but I don't understand why it prints even before any of the above conditions are met.

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    cout << "Test";
    float secs = 5;
    clock_t delay = secs * CLOCKS_PER_SEC;
    clock_t start = clock();
    while (clock() - start < delay) { }
    return 0;
}

When run, "Test" is output before the loop begins.

Why is my output not buffered until the program terminates?

Lecher answered 18/12, 2017 at 17:11 Comment(3)
Try std::ios::sync_with_stdio(false); before you print.Mccaskill
std::ios::sync_with_stdio(false); works great, thanks! But why?Beetroot
See my answer belowMccaskill
M
4

There's a great discussion on this over here.

From one of the answers:

Every C++ stream uses an associated stream buffer object to perform buffering.

When std::cout is constructed, it uses the stream buffer associated with the object stdout, declared in <cstdio>. By default, operations on std::cout can be freely mixed with <cstdio> output functions like std::printf().

In practical terms, synchronization usually means that a standard iostream object and a standard stdio object share a buffer. - IS

If std::ios_base::sync_with_stdio(false) is called (before any input or output operations on the standard streams), the standard C++ streams operate independently of the standard C streams (ie. they switch to their own separate stream buffers).

For more, see the sync_with_stdio function reference page on cppreference.

From that page, the function...

Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.

...In practice, this means that the synchronized C++ streams are unbuffered, and each I/O operation on a C++ stream is immediately applied to the corresponding C stream's buffer. This makes it possible to freely mix C++ and C I/O.

However, beware of calling this function after there have already been reads or writes:

If this function is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.

Mccaskill answered 18/12, 2017 at 17:24 Comment(0)
A
2

There is also another great conversaion here. This seems related to some of the things scohe001 mentioned, however is a little different, so I'll put it in it's own answer.

Related to the above answer is this post on that forum, which talks about how the buffer is flushed depending on other surrounding code. The Std::cout function is tied with other stream functions, and normal c library functions as scohe001 mentioned. So if something is called that it is tied to, its buffer will flush before continuing.

Are you compiling this with gcc on Linux, or running this in some windows environment? This post here from that forum above talks about OS specific functions, and that the sleep() from windows might cause the buffer to flush out. Otherwise normal gcc compiled c++ code would not print the buffer using sleep(), so long that it hasn't encountered any other code that would flush the buffer before continuing.

These above posts cover a lot of information, so I'll refrain from copy and pasting it here, so please forgive me stackoverflow gods.

I hope this info helps out!

Angelangela answered 18/12, 2017 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.