Why my print didn't output before a segmentation fault
blog.yelinaung.com
blog.yelinaung.com
If you want to have visible debug information, you can either:
- call << std::flush;
- use << std::endl; which is basically equivalent to << "\n" << std::flush;
- use std::cerr instead of std::cout, which is unbuffered.
``` invokeUB(); doSomething(); ```
and regardless of what `doSomething()` does (it need not be a function call), remove the call entirely if it so chooses. The reason for this is that `doSomething()` cannot be reached except after `invokeUB()`. Because undefined behaviour can be considered unreachable, `invokeUB()` can be too. Therefore, to the compiler, it is impossible to ever reach `doSomething()`, and thus it may be safely eliminated.
In practical terms, compilers are allowed to reorder calculations, and the print might as well not be there if it's after a crash.
In theoretical terms, once UB is guaranteed to happen then your program execution is no longer valid, the entire execution, and the compiler is not bound by any rules whatsoever.
xterm -hold -e './a.out' - no segfault
xterm -hold -e '(./a.out)' - will print segfault
stdbuf -i0 -o0 -e0 /some/command
Where -i is for stdin, -o is for stdout, and -e is for stderr, and the number is the buffer size.