NULL
is not type safe. For historical reason it was defined as 0 without casting, and the compiler silence warning of casting number to pointer on this special zero.
For instant, you can do:
void* p = 0;
but not this without implicit casting:
void* p = 1234;
the side effect is that it can be abused as number values, as other answer mentioned.
nullptr
improve this by enforcing it is a pointer, you can't assign this to an integer.
Since the behaviour is changed, a new name is created for backward compatibility.
Also note that, nullptr
is handled by the compiler, its actual value is not exposed to user (like zero in case of NULL
). It's much easier to have architecture dependent value, say 0xdeadbeef
, without affect programmer's code logic.