NULL vs 0

Started by
43 comments, last by wqking 11 years, 4 months ago
Basically the reason that nullptr exists is because the type of NULL isn't a pointer type, it's an integer constant expression. If you have an overloaded function void foo(int); and void foo(void *); then foo(NULL) calls the int overload rather than the void pointer overload despite the fact that NULL has the implied meaning of a pointer constant. Not a big deal in C when you don't have function overloads, but becomes a problem in C++ where not only do you have function overloads, you have weird template type deductions and what not because NULL is an integer constant.
Advertisement
SiCrane - I don't think it's true.
GCC (also Visual Studio) defines NULL like this: "#define NULL ((void *)0)" in stddef.h.
So:
c:\ test>type a.cpp

#define NULL ((void*)0)
void foo(int);
void foo(void*);
int main()
{
foo(NULL);
}

c:\ test>g++ a.cpp
C:\Users\XXX\AppData\Local\Temp\ccAUKoxR.o:a.cpp:(.text+0x16): undefined reference to `foo(void*)'
collect2.exe: error: ld returned 1 exit status


foo(NULL) correctly calls foo(void*).
You know, I've always wondered, when they created the C++11 spec, why did they create "nullptr" instead of just using the reserved but functionless "null" keyword that already exists?

Was it because people were creating their own functionality for it via macros or what have you?
I used NULL in the past, but now I'd just define nullptr if it wasn't available. So that when you transition to a modern compiler, your code base stays consistent.

You know, I've always wondered, when they created the C++11 spec, why did they create "nullptr" instead of just using the reserved but functionless "null" keyword that already exists?

Was it because people were creating their own functionality for it via macros or what have you?


"null" is a keyword in C++? I didn't know that... Or are you thinking of a different language?

SiCrane - I don't think it's true.
GCC (also Visual Studio) defines NULL like this: "#define NULL ((void *)0)" in stddef.h.

((void *)0) is not a legal definition for NULL in C++. It is a valid definition for NULL in C. So MSVC's NULL definition looks like:

#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *)0)
#endif

If you try using the ((void *)0) definition in C++ you get errors doing simple things like int * p = NULL; since a void * can't be assigned to a non void pointer without a cast in C++. See sections 18.1 in the C++98 and C++03 and 18.2 in the C++11 standards where the footnotes specifically say that (void *)0 is not a legal definition for NULL.

[quote name='Flimflam' timestamp='1355524803' post='5010765']
You know, I've always wondered, when they created the C++11 spec, why did they create "nullptr" instead of just using the reserved but functionless "null" keyword that already exists?

Was it because people were creating their own functionality for it via macros or what have you?


"null" is a keyword in C++? I didn't know that... Or are you thinking of a different language?
[/quote]
No, not officially. My wording was a bit misleading. In fact thinking back, I'm just confusing the functionality of an old compiler that had reserved it. I'm unaware if any others have since.

((void *)0) is not a legal definition for NULL in C++. It is a valid definition for NULL in C. So MSVC's NULL definition looks like:

Yes, you're right. Sorry, I misread that #ifdef...
I actually was unpleasantly surprised recently when I was writing templates with no header inclusions and I discovered that NULL is not a keyword. (I always thought it was part of the language for some reason.)

In such files I took to saying:
[source lang="cpp"]#ifndef NULL
#define NULL 0
#endif[/source]

But I'm not using C++11 at the moment, so I don't have nullptr. There's some interesting things to consider in this thread, though. Thanks for starting this, SotL.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Always NULL.
There are plenty of reasons, I listed some reasons in my early blog.
http://cpgf.org/blog/why-i-prefer-null-over-0-as-null-pointer.html

My primary three points,
1, Reason 1, NULL is self explained.
2, Reason 2, C++ standard saying NULL is 0 doesn't mean null pointer is 0
3, 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?

And I have tough time to conver C++ code with 0 as pointer to Lua code, but maybe only I do it :)

However, nothing is correct or wrong, it is very subjective opinion, IMHO.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement