Why does cout output immediately?

cout is a buffer stream. This means that data will be written to the buffer and printed when the stream is cleared, the program is complete, or when the buffer is full.

I made a small program to check how this works, but I don’t understand why it is printed 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 you start the "Test" is displayed before the start of the cycle.

Why is my output not buffered until the program terminates?

+5
source share
2 answers

There is a lot of discussion about this issue here .

From one of the answers:

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

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

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

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

See the sync_with_stdio man page for sync_with_stdio for details .

From this page, the function ...

Sets whether standard C ++ streams synchronize with standard C streams after each I / O operation.

... In practice, this means that the synchronized C ++ streams are unbuffered, and each I / O operation in the C ++ stream is immediately applied to the corresponding buffer of stream C. This allows you to freely mix C ++ and CI / O.

However, be careful when calling this function after they have already been read or written:

If this function is called after I / O has occurred in the standard stream, the behavior is determined by the implementation: implementations have no effect on destroying the read buffer.

+4
source

There is also another great conversation here . This seems to be related to some of the ones mentioned in scohe001, however it is slightly different, so I will put it in my own answer.

In connection with the above answer, this is a post on this forum that talks about how the buffer is flushed depending on other surrounding code. The Std :: cout function is bound to other stream functions, and regular c libraries are bound as scohe001. Therefore, if something is called that it is bound, its buffer will be hidden until continued.

Do you compile this with gcc on Linux or run it in some windows? This post here from this forum above talks about specific OS functions and what sleep () from windows can cause a buffer reset. Otherwise, regular gcc-compiled C ++ code will not print the buffer using sleep (), so it will not encounter any other code that will clear the buffer before continuing.

These messages contain a lot of information, so I will refrain from copying and pasting it here, so please forgive me gods of steakovert.

I hope this information helps!

+2
source

Source: https://habr.com/ru/post/1274163/


All Articles