Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a simple C++ program in which std::cout is not flushed

In an effort to better understand buffered streams in C++, I would like to write a simple program in which the std::cout buffer is NOT flushed before termination. Since I have read that std::cout is flushed on normal termination, I tried throwing a runtime error. I also avoided using std::endl, as I understand that forces a flush. First attempt:

//file noflush.cpp
#include <iostream>

int main() {
    std::cout << "Don't write me to the console!";
    throw 0;
}

Compile with g++, call from terminal:

$ ./noflush
libc++abi.dylib: terminating with uncaught exception of type int
Don't write me to the console!Abort trap: 6

Even when I force a runtime error, it seems the buffer still gets flushed on termination. Is it possible to "strand" some data in the buffer, leaving it unwritten to the device?

like image 770
Chad Avatar asked Jun 01 '16 07:06

Chad


People also ask

Does cout flush automatically?

It is an implementation detail, one that invariably depends on whether output is redirected. If it is not then flushing is automatic for the obvious reason, you expect to immediately see whatever you cout. Most CRTs have an isatty() helper function that is used to determine whether automatic flushing is required.

Does cout flush?

The predefined streams cout and clog are flushed when input is requested from the predefined input stream (cin). The predefined stream cerr is flushed after each output operation. An output stream that is unit-buffered is flushed after each output operation.

Why is std :: cout not working?

There is no std::cout in C. In a windowing system, the std::cout may not be implemented because there are windows and the OS doesn't know which one of your windows to output to. never ever give cout NULL. it will stop to work.


2 Answers

This is not standard c++, but in POSIX, you can send a "kill" signal to kill the running process. This will stop the execution without cleanup such as flushing buffers.

Edit: I realized that signals are not only POSIX but actually part of C standard library (and included in the C++ standard library).

#include <csignal>
// ...
std::cout << "Don't write me to the console!";
std::raise(SIGKILL);
like image 112
eerorika Avatar answered Oct 19 '22 19:10

eerorika


As far as I can tell, there is no standard compliant and clean way to avoid std::cout to flush() before program termination (but, of course, you can use unclean methods, e.g. raising a signal either directly or indirectly). According to cppreference, the actual buffer type controlled by std::cout is implementation defined but derived from std::streambuf, which does not appear to allow public access in a way that would emulate silent swallowing of the buffer.

Further, as I noted in a comment, even abnormal program termination (via std::terminate() or std::abort() may or may not close open resources, so this is again implementation defined.

like image 39
Walter Avatar answered Oct 19 '22 18:10

Walter