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?
From crtdbg.h (and many other headers):
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif
#endif
Therefore NULL
is 0
, at least on the Windows platform. So no, not that I know of.
I never use NULL in my C or C++ code. 0
works just fine, as does if (ptrname)
. Any competent C or C++ programmer should know what those do.
In C++ NULL expands to 0 or 0L. See this comment from Stroustrup:
Should I use NULL or 0? 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. 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 going to be called in C++0x. Then, "nullptr" will be a keyword.
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:
void Foo(int i);
void Foo(Bar* b);
void Foo(bool b);
main()
{
Foo(0);
Foo(NULL); // same as Foo(0)
}
C++0x defines a nullptr
that is convertible to a null pointer but not to other scalars. VC++ supports this from 2008 (could be earlier). GCC has had something like this for ages but it is there called __null
.
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 have nullptr
available so the question won't matter as much anymore.