Do you use NULL or 0 (zero) for pointers in C++?
In the early days of C++ when it was bolted on top of C, you could not use NULL as it was defined as (void*)0
. You could not assign NULL to any pointer other than void*
, which made it kind of useless. Back in those days, it was accepted that you used 0
(zero) for null pointers.
To this day, I have continued to use zero as a null pointer but those around me insist on using NULL
. I personally do not see any benefit to giving a name (NULL
) to an existing value - and since I also like to test pointers as truth values:
if (p && !q)
do_something();
then using zero makes more sense (as in if you use NULL
, you cannot logically use p && !q
- you need to explicitly compare against NULL
, unless you assume NULL
is zero, in which case why use NULL
).
Is there any objective reason to prefer zero over NULL (or vice versa), or is all just personal preference?
Edit: I should add (and meant to originally say) that with RAII and exceptions, I rarely use zero/NULL pointers, but sometimes you do need them still.
Solution 1:
Here's Stroustrup's take on this: C++ Style and Technique FAQ
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 withNULL
is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code,NULL
was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.If you have to name the null pointer, call it
nullptr
; that's what it's called in C++11. Then,nullptr
will be a keyword.
That said, don't sweat the small stuff.
Solution 2:
There are a few arguments (one of which is relatively recent) which I believe contradict Bjarne's position on this.
-
Documentation of intent
Using
NULL
allows for searches on its use and it also highlights that the developer wanted to use aNULL
pointer, irrespective of whether it is being interpreted by the compiler asNULL
or not. -
Overload of pointer and 'int' is relatively rare
The example that everybody quotes is:
void foo(int*); void foo (int); void bar() { foo (NULL); // Calls 'foo(int)' }
However, at least in my opinion, the problem with the above is not that we're using
NULL
for the null pointer constant: it's that we have overloads offoo()
which take very different kinds of arguments. The parameter must be anint
too, as any other type will result in an ambiguous call and so generate a helpful compiler warning. -
Analysis tools can help TODAY!
Even in the absence of C++0x, there are tools available today that verify that
NULL
is being used for pointers, and that0
is being used for integral types. -
C++ 11 will have a new
std::nullptr_t
type.This is the newest argument to the table. The problem of
0
andNULL
is being actively addressed for C++0x, and you can guarantee that for every implementation that providesNULL
, the very first thing that they will do is:#define NULL nullptr
For those who use
NULL
rather than0
, the change will be an improvement in type-safety with little or no effort - if anything it may also catch a few bugs where they've usedNULL
for0
. For anybody using0
today... well, hopefully they have a good knowledge of regular expressions...
Solution 3:
Use NULL. NULL shows your intent. That it is 0 is an implementation detail that should not matter.
Solution 4:
I always use:
-
NULL
for pointers -
'\0'
for chars -
0.0
for floats and doubles
where 0 would do fine. It is a matter of signaling intent. That said, I am not anal about it.
Solution 5:
I stopped using NULL in favor of 0 long ago (as well as as most other macros). I did this not only because I wanted to avoid macros as much as possible, but also because NULL seems to have become over-used in C and C++ code. It seems to be used whenever a 0 value is needed, not just for pointers.
On new projects, I put this in a project header:
static const int nullptr = 0;
Now, when C++0x compliant compilers arrive, all I have to do is remove that line. A nice benefit of this is that Visual Studio already recognizes nullptr as a keyword and highlights it appropriately.