Possible Duplicate:
Do you use NULL or 0 (zero) for pointers in C++?
Is it a good idea to use NULL in C++ or just the value 0?
Is there a special circumstance using NULL in C code calling from C++? Like SDL?
Possible Duplicate:
Do you use NULL or 0 (zero) for pointers in C++?
Is it a good idea to use NULL in C++ or just the value 0?
Is there a special circumstance using NULL in C code calling from C++? Like SDL?
The downside of NULL in C++ is that it is a define for 0. This is a value that can be silently converted to pointer, a bool value, a float/double, or an int.
That is not very type safe and has lead to actual bugs in an application I worked on.
Consider this:
C++11 defines a
nullptr
that is convertible to a null pointer but not to other scalars. This is supported in all modern C++ compilers, including VC++ as of 2008. In older versions of GCC there is a similar feature, but then it was called__null
.In C++ NULL expands to 0 or 0L. See this comment from Stroustrup:
Assuming that you don't have a library or system header that defines
NULL
as for example(void*)0
or(char*)0
it's fine. I always tend to use 0 myself as it is by definition the null pointer. In c++0x you'll havenullptr
available so the question won't matter as much anymore.I never use NULL in my C or C++ code.
0
works just fine, as doesif (ptrname)
. Any competent C or C++ programmer should know what those do.From crtdbg.h (and many other headers):
Therefore
NULL
is0
, at least on the Windows platform. So no, not that I know of.