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?
std::abort()
the buffer is flushed, (using Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn) with LLVM libc++), though according to cppreference it is implementation defined whether open resources such as files are closed (upon callingstd::abort()
). – Bencion