Curious about cout when there is a bus error on some next line

574 Views Asked by At

I am curious about how cout work with bus/segmentation errors. I have two examples pasted below. Because I don't know how to replicate a bus error you have to take if from me that grid.DoMovement, in this example, throws a bus error. While I was cout-ing something before the bus error line I noticed that if I put an endline in it will print it out fine, but if I don't put an endl in it does not. The examples at the bottom show what I mean.
Why is it that if you don't put an endline in the cout and you have a bus error on one of the later lines that it doesn't print out the "example 2"?

Example 1:

std::cout << "example 1" << endl;
grid.DoMovement(); 

Output is

works
bus error

Example 2:

std::out << "example 2";
grid.DoMovement(); 

Output is

bus error
4

There are 4 best solutions below

0
On BEST ANSWER

By default IOStreams are buffered. Anything only written to the buffer won't be displayed. When you use std::endl with a stream a newline gets added and the stream is flushed. Note, that normally you don't want to have the stream flushed: Frequent flushing of flie streams can reduce performance dramatically! Thus, it is a good idea not to use std::endl but '\n' if you want a newline. If you really want to flush a stream you can explicitly use std::flush which just flushes the stream.

During debugging it may be helpful to have all output appear as soon as it is written so that a crash doesn't prevent output from showing. If you follow the above advise and don't flush frequently a lot of output may get buffered. The simple remedy for this is to use std::unitbuf: this manipulator turns on the flag std::ios_base::unitbuf cause the output streams to be flushed after each insertion. You can turn the flag off again using std::nounitbuf to avoid slowdown in code sections which are known to work (or, at least, known not to fail in a too dramatic way):

std::cout << std::unitbuf;   // turn on automatic flushing
problematic_code();
std::cout << std::nounitbuf; // turn off automatic flushing

The default settings for the std::ios_base::unitbuf is the differences between std::cerr and std::clog: both stream write to the standard error stream (on UNIXes file descriptor 2) but std::cerr flushes its buffer after each write while std::clog does not.

0
On

std::cout is buffered when std::cerr is not. If you tests with std::cerr, with or without std::endl, you'll see your message.

http://www.cplusplus.com/reference/iostream/manipulators/endl

0
On

The std::endl does not only add a new line to the stream, it also flush the current buffer of the output stream. If you get a bus error while some data is in the output stream buffer, you won't see the data.

0
On

endl is a "magical" manipulator that outputs the newline character and flushes the output buffer.

The buffer is also normally flushed when the program finishes, so you usually cannot see the additional effect of endl. But if your program crashes shortly thereafter, the buffers are not flushed, and the output doesn't appear.

You can change your second example to

std::out << "example 2" << flush;

to see the effect of flushing.