Possible Duplicate:
Why does std::cout output disappear completely after NULL is sent to it
It seems if you try:
std::cout << NULL << endl;
std::cout << "hell" << endl;
it print out nothing and C++ IO stops working for all subsequent outputs.
but it works fine in C stdio:
printf("%s\n", NULL);
printf("%s\n", "hell");
(null)
hell
Is there any good reason why C++ IO can’t do the same thing?
(edited in response to comments)
alright, to make it clear, NULL does have a type, say const char*
const char* getxxx(); // may return NULL,
cout << getxxx(); // won't work if NULL returned
Huh? I see no reason why
coutshould fail simply because you executedIt should output
0\n. And it does. End of story.(In case you’re confused, please know that in C++,
#define NULL (0).)In case you wrote:
then it will display the address
0, (generally in hexadecimal and padded to the pointer size, since this is the preferred way of looking at pointers).(This is btw the behavior you would get using the C definition of NULL, which is
#define NULL ((void*)0).)Only if you write
are you in trouble. Now you’re calling
for which the Standard (section 27.7.3.6.4) says:
When you do pass a null pointer, the rule 17.6.4.9 applies, which states that:
So you’re in the land of “undefined behavior”. There’s no guarantee that
failbitgets set and the program continues.Please note that
printfbehavior didn’t actually depend on the type ofNULL. It’s the format string"%s"that caused treatment as a string (pointer to NUL-terminated character sequence).