0
$ g++ --version
g++ (GCC) 9.2.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    
$ g++ test.cpp -o test -g

test.cpp

#include <iostream>

int main() {
  const char * c = nullptr;
  std::cout << "testing: " << c << std::endl;
  std::cout << "why does this not print?" << std::endl;
  return 0;
}

Running test:

$ ./test
testing: $ echo $?
0

It seems like the nullptr prevents further printing to stdout but I'm not sure why. The std::endl does not seem to be processed, and the same thing occurs if I just pass \n instead of std::endl.

ajoseps
  • 1,871
  • 1
  • 16
  • 29
  • Garbage in, garbage out. Your promised `cout` a c-string and it got nothing, and now you have undefined behavior. – NathanOliver Feb 23 '23 at 15:10
  • 1
    You don't pass nullptr, it has own type, you pass a null pointer. https://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2 *The behavior is undefined if s is a null pointer.* – 273K Feb 23 '23 at 15:12
  • Upon breaking the stream, you can't expect it to work if you don't fix it. They are not self-healing. – sweenish Feb 23 '23 at 15:12
  • I see it's undefined behavior. I'm surprised the rest of the program continues working, but I guess that is the nature of undefined behavior – ajoseps Feb 23 '23 at 15:14
  • 2
    @AdrianMole You use a wrong reference. It says *Character and character string arguments (e.g., of type char or const char\*) are handled by the non-member overloads of operator<<.* – 273K Feb 23 '23 at 15:22
  • @273K Ah - OK. I was wondering why there were two different versions. – Adrian Mole Feb 23 '23 at 15:22
  • 1
    See dupe: [Why does std::cout output disappear completely after NULL is sent to it](https://stackoverflow.com/questions/7019454/why-does-stdcout-output-disappear-completely-after-null-is-sent-to-it) – Jason Feb 23 '23 at 15:24

0 Answers0