tags:

views:

642

answers:

8

Hey,

I know that you are supposed to use 0 instead of NULL in c++ (even though NULL is defined as 0 in c++ most of the time). Recently I came across some code where "0x0" was used instead though. What is the difference?

Thank you!

+16  A: 

0x0 is just 0 written in hexadecimal notation. There is no difference between the two:

016 = 010 :)

NULL is usually #defined to 0 somewhere and does the same thing.

Mehrdad Afshari
+2  A: 

0x0 is just an expression of the value 0 in hexadecimal. I doubt the compiler will care about the difference.

postfuturist
+1  A: 

No difference

In my opinion, 0x0 is more explicit.

Some people may confuse 0 with "0"(0x30 -I´ve seen it happen).

Tom
0x0000 is even more explicit!
Roger Pate
@Roger: nah, that's only more explicit on a 16bit machine. On a 32bit machine it's still not explicit enough ;-)
Steve Jessop
+1 for giving me a laugh. But if you are running a 64 bit compiler you should use 0x0000000000000000 for maximum explicitness.
drhirsch
watch out you don't get banned for that kind of explicit language, folks...
Eamon Nerbonne
Actually, on a 64 bit machine it may make sense to define NULL as 0LL, so `sizeof(NULL)==sizeof((void*)NULL)`.
MSalters
A: 

So, this site has some really good information on NULL: http://c-faq.com/null/index.html

NULL is actually the "null pointer" - so it might be of type, for example, "(void*)0". This tells the compiler that NULL is not to be treated as an int, but rather as a "pointer to an int". And in C, the compiler will give warnings/errors if the types don't match.

The value "0" and "0x0" are equivalent in C++. Whenever a number is prefixed by "0x", it means that that value is the hexadecimal representation of that number. For example, 0x5 == 5.

Also, 0xA [in hexadecimal, base 16) == 10 [in base 10].

edit

Here are some sources. In glibc, NULL seems to be defined differently in different places. (why should this be?)

#define NULL ((void *)0) (stdlib/gmp-impl.h)

#define NULL 0 (posix/getopt1.c)

edit2

well, it looks like i've missed the mark on this one! sorry! (CW'd. feel free to keep downvoting!)

rascher
In C++, NULL will not be (for example) "(void*)0" -- that's clearly not allowed. Though it's probably a typo, that would be treated as point to void, not pointer to int.
Jerry Coffin
This answer is wrong. NULL is not the "null pointer". It merely converts to a "null pointer" of a particular type. Also you are linking to a C site on a C++ question. (even in C, NULL is not necessarily a "null pointer").
Johannes Schaub - litb
Notice that your linked site says "NULL is defined as a null pointer constant" - *not* as a null pointer!
Johannes Schaub - litb
+5  A: 

0x0 and 0 represent the same value, so you can use the one that best suits your eye. When we get C++0x, we'll have the keyword nullptr to represent null pointers of any type.

Kristo
+16  A: 

There is no difference. In C, NULL is often defined to be (void *)0, but in C++ that's not allowed. A few ancient compilers got this wrong, but they really are ancient.

IMO, it's better to use NULL, as it portrays the intent more clearly, and gives you a nice, easy symbol to S&R when your compiler gets updated to C++ 0x, which will include nullptr.

Jerry Coffin
+1. Up to very recently I was party to using 0 instead of NULL, but the inclusion of `nullptr` in C++0x made me change my mind.
Gorpik
A: 

Simply put, its a matter of taste. People like to write memory addresses in hexa decimal, so writing NULL as 0x0 would be more classic. For ordinary ones like me, 0 will suffice.

Shailesh Kumar
+4  A: 

By the way, everyone,

0x0 is hex, as you have all mentioned, but

0 is Octal, not decimal! :-)

ie any number starting with 0 (and not followed by x) is octal:

0 = 0
01 = 1
02 = 2
...
07 = 7
010 = 8
011 = 9
012 = 10
...

:-)

tony
I was going to down vote you for sowing confusion. But I had to check the standard first ... and you're right. Learn something new everyday (not that this is worth remembering, but I will anyways).
caspin
that's exactly how it lives in my brain - I know it is totally useless, but yet I remember it.
tony