When studying the sample code for this question I had assumed it was Undefined Behaviour which was preventing subsequent uses of std::cout
from printing. But it turns out that attempting to print a null pointer caused std::ios_base::badbit
and std::ios_base::failbit
to be set in its stream state which was the real cause for its being non-operational. Because of this, I am now curious if it really is Undefined Behaviour to (attempt) to print a null-pointer. So here are my questions:
Is it Undefined Behaviour to print a null-pointer? If so, what is it about the stream inserter that would cause this? I'm pretty certain the inserter is smart enough to not dereference a null-pointer.
I would also like to know why the inserter sets its error mask when encountering a null-pointer in this context (specifically
badbit
). Why doesn't it treat it like the termination of a string literal?
I don't have a Standard handy, and I only found one source thus far that unfortunately led to a dead link.