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!
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!
0x0
is just 0
written in hexadecimal notation. There is no difference between the two:
016 = 010 :)
NULL
is usually #define
d to 0
somewhere and does the same thing.
0x0 is just an expression of the value 0 in hexadecimal. I doubt the compiler will care about the difference.
No difference
In my opinion, 0x0 is more explicit.
Some people may confuse 0 with "0"(0x30 -I´ve seen it happen).
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!)
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.
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
.
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.
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 ...
:-)