Why I prefer NULL over 0 as null pointer in C++

Today I added a new item in cpgf FAQ documentation about why 0 can be passed as pointer in Google V8 Javascript and Python, but not in Lua.

I add that entry because I found the same problem when I convert a sample code for Irrlicht engine from Javascript to Lua. In the Irrlicht sample code, 0 is used as null pointer. I'm not familiar with Irrlicht and don't know which method parameter is pointer and which is integer, so when I was converting Irrlicht C++ sample code to Javascript, I just keep 0 as it is, but when I convert the code from JS to Lua, 0 doesn't work any more. For the reason, please read the FAQ, I won't repeat myself.

That small problem makes me confirm that it's correct that I always use NULL rather than 0 as null pointer. Below I will explain it.

Why do I prefer NULL over 0 in C++

Reason 1, NULL is self explained.

Assume we have a function isSamePointerAt to check if a given pointer is same as the element at a give index. Then if we want to check if the element at 0 is a null pointer, if we call the function as “isSamePointerAt(0, 0);”, before we seeing the declaration of isSamePointerAt, do we know the function prototype even we already know one parameter is pointer and another one is integer? No, we don't know.

If we can the function as “isSamePointerAt(0, NULL);”, OK, now we know the prototype is “bool isSamePointerAt(int, void *);”, or at least the first parameter is the index and the second is the pointer.

The later version code is self explained, aka, self documented, while the former version is not. Do we want our code self explained? Yes! So I use NULL.

Reason 2, C++ standard saying NULL is 0 doesn't mean null pointer is 0

Null pointer is a pointer, though its literal value is zero. Null pointer has essential difference with an integer 0. For example, with integer 0, we can do any mathematics such as 5 & 0, a * 0, etc, while it's quite strange if we write 5 & NULL, a * NULL.

So if null pointer is not 0, then why NULL, the alias for null pointer, can be equivalent to 0?

Reason 3, C++11 supports nullptr, everyone will prefer nullptr to 0, then why don't we use NULL than 0 in pre-C++11 era?

I know C++11 is the current standard. But cpgf is still written in older C++ standard because not all main stream compilers support C++11.

If all of the world are using C++11, I believe nobody will use 0 instead of nullptr, because nullptr is the standard. In pre-C++11 era, NULL is the very common convention though it's not standard, then why don't we use NULL instead of 0, same as we use nullptr instead of 0 in C++11?

It is more important to use NULL if you will convert C++ code to script

No matter you use cpgf library or not, if you are going to convert your C++ code to script, it's really helpful if you start using NULL instead of 0 as null pointer, otherwise, you may need to spend hours on hunting bugs when converting your code to a script language which doesn't support integer data type, such as Lua.

If you use NULL, when converting the code to script, you can always know it's a pointer and convert it to null, nil, or None, which is the null pointer in script. If you use 0, you may forget that it's a pointer and just convert as 0 to script. Script doesn't treat 0 as pointer, that may be quite painful.


Enter your comment: