10

I was wondering if NULL is guaranteed to be 0 in C++, so I searched and came across these:


This answer states that:

Here is Bjarne Stroustrup's wordings,

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer.

Which seems to confirm that NULL is always 0.


But according to cppreference.com:

#define NULL /*implementation-defined*/

The macro NULL is an implementation-defined null pointer constant, which may be:

-> an integral constant expression rvalue of integer type that evaluates to zero (until C++11)

-> an integer literal with value zero, or a prvalue of type std::nullptr_t (since C++11)

And that clearly says NULL is implementation dependent.


This answer says:

0 (aka. C's NULL bridged over into C++) could cause ambiguity in overloaded function resolution, among other things:

f(int); 
f(foo *);

Which again implies that NULL is the integer 0 and that might cause ambiguity


There are other questions and answers I encountered, but they are mostly about the C language, not C++. This comment says:

And NULL is guaranteed to be 0

But again, that's about C.


To sum it all up, is NULL always 0 in C++? What about C? Is it (for every standard implementation) the same as:

#define NULL 0

Note: this question is not about the null pointer, the question is if NULL in C++ is guaranteed to be 0 the integer. Is it implementation dependent?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 1
    I recommend searching StackOverflow, then removing this question if it is a duplicate. – Thomas Matthews Jan 08 '19 at 19:51
  • 1
    @ThomasMatthews I added everything relevant I found on SO. Can you share some of the relevant questions you came across? – Aykhan Hagverdili Jan 08 '19 at 19:53
  • It's hard to understand exactly what you're asking. Are you asking if `0` and `NULL` must behave the same in all contexts if those literals appear in code? Are you asking if `NULL`, if converted to type `int` must have the value zero? You say things like "is NULL always 0" as if that had an unambiguous meaning and I don't think it does. – David Schwartz Jan 08 '19 at 19:53
  • 1
    @DavidSchwartz if it's exactly the same as `#define NULL 0` – Aykhan Hagverdili Jan 08 '19 at 19:55
  • 1
    That first quote from Stroustrup is **really, really old**. It is no longer accurate. – Pete Becker Jan 08 '19 at 19:58
  • 4
    @George -- `(void*)0` is not a valid null pointer constant in C++; there is no implicit conversion from `void*` to any other pointer type. It's okay in C. – Pete Becker Jan 08 '19 at 19:58
  • By your request, seach keys: [stackoverflow c++ null pointer guranteed 0](https://www.google.com/search?q=stackoverflow+c%2B%2B+null+pointer+guranteed+0&rlz=1C1GCEA_enUS830US830&oq=stackoverflow+c%2B%2B+null+pointer+guranteed+0&aqs=chrome..69i57j69i64.15086j1j7&sourceid=chrome&ie=UTF-8) – Thomas Matthews Jan 08 '19 at 20:01
  • @ThomasMatthews almost everything from that Google search is either about C, or "Is NULL the same as null pointer" – Aykhan Hagverdili Jan 08 '19 at 20:05

2 Answers2

9

Is NULL guaranteed to be 0?

According to the standard, NULL is a null pointer constant (i.e. literal). Exactly which one, is implementation defined.

Prior to C++11, null pointer constants were integral constants whose integral value is equal to 0, so 0 or 0l etc.

Since C++11, there is a new null pointer literal nullptr and NULL may be defined as being nullptr. (And thus literal interpretation of Bjarne's quote has become obsolete).

Prior to standardisation: NULL may be defined as (void*)0 in C. Since C++ was based on C, it is likely that some C++ dialects pre-dating the standard might have used that definition, but such definition is not conformant with standard C++.


And for completeness: As explained in more detail in SO post linked in a comment below, null pointer constant being 0 does not necessarily mean that the value of the null pointer address is 0 (although the address being 0 is quite typical).

What can be concluded about this:

  • Don't use NULL to represent the number zero (use 0 with appropriate type suffix if appropriate), nor to represent a null-terminator character (use '\0').
  • Don't assume that NULL resolves to a pointer overload.
  • To represent a null pointer, don't use NULL but instead use nullptr if your standard is >= C++11. In older standard you can use (T*)NULL or (T*)0 if you need it for overload resolution... that said there are probably very few cases where overloading integers with pointers makes any sense.
  • Consider that the definition may differ when converting from C to C++ and vice versa.
  • Don't memset (or type pun) zero bits into a pointer. That's not guaranteed to be the null pointer.
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • "*Prior to C++11, null pointer constants were integral constants whose value is equal to 0, so 0 or 0l etc.*" But even then it was implementation dependent, am I right? – Aykhan Hagverdili Jan 08 '19 at 20:00
  • 1
    It was as it still is implementation defined which one of the null pointer constants would be used to define `NULL`. One implementation might use `0`, another might use `0l` - and now it may be `nullptr`. – eerorika Jan 08 '19 at 20:01
  • 3
    [What if you have a machine with nonzero bit pattern for nullptr?](https://stackoverflow.com/a/2761408/2466431) – JVApen Jan 08 '19 at 20:07
  • 1
    Don't assume memset(struct,0,) initialized your pointers to nullptr either. Good luck tracking that bug down 10 years from now when you port to ARM. It won't be fun. – Joshua Jan 09 '19 at 05:46
0

I think its fine practice to assume so. Having said that, it would be horrible practice to #undef NULL and or redefine it as something else. So long story short, it's not a 100% guarentee because it is possible to be something else, but it NEVER should be.

Mike
  • 190
  • 7
  • If you have the choice, it should probably be `nullptr`, not `0`, or some other zero integer-literal. – Deduplicator Jan 15 '19 at 16:02
  • Depending on my user defined functions and how they return, sometimes i.check against NULL in conditionals. Maybe its bad practice, but Ive never had an issue. – Mike Jan 15 '19 at 16:07