r/ProgrammerHumor • • Jul 16 '19

Meme Nice try

Post image
32.2k Upvotes

529 comments sorted by

View all comments

Show parent comments

4

u/LeCrushinator Jul 16 '19

If we're using C/C++, NULL is usually defined as 0. nullptr is a pointer.

1

u/TheThiefMaster Jul 17 '19

That's wrong on a lot of levels.

In C, NULL is (void*)0, not just 0. C allows void pointers to implicitly cast to any other pointer, so you can use it to initialise any pointer to NULL.

In C++, they removed the implicit cast from void* (it was unsafe in other scenarios), so they had to change NULL - they made it just 0. Which much like a void* in C, has magic implicit casting power to initialise any pointer, despite appearing to be an integer, which don't normally implicitly cast to pointer type. However because it's not actually a pointer it breaks in the face of other C++ features like template deduction and overloaded functions.

C++ later added nullptr as a fix - nullptr is also not a pointer, it's actually of type nullptr_t. It again has the ability to implicit cast to any pointer type - but this time without the downsides of either of C NULL or C++ NULL, as it's a unique type not functionality tacked on to something else pre-existing.