4

As mentioned in the question, I have been using NULL and false(in C++) interchangeably with 0 or 0x0 and so on. I was curious to know if they held any special meaning other than being synonyms of 0.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
CuriousSid
  • 534
  • 3
  • 11
  • 25
  • 2
    So you did stuff like `int* x = false`? – Luchian Grigore Mar 25 '13 at 13:30
  • NULL is a pointer to a memory location, usually 0x00 although I think that depends on the platform. True and false are usually just defined to be 1 and 0 values in C++. – austin Mar 25 '13 at 13:31
  • @austin: actually, `NULL` is just an integral constant expression with the value of `0`. –  Mar 25 '13 at 13:32
  • [`NULL` is (was) not always `0`](http://stackoverflow.com/questions/2597142/when-was-the-null-macro-not-0). – Frédéric Hamidi Mar 25 '13 at 13:32
  • Similar to http://stackoverflow.com/questions/2053709/is-null-the-same-as-false-in-c and http://stackoverflow.com/questions/459743/is-null-always-false – Shafik Yaghmour Mar 25 '13 at 13:33
  • @Fanael hmm.. yeah didn't think that it's basically just a value. Potentially, you could do something like int k = NULL + NULL; – austin Mar 25 '13 at 13:34
  • I edited the title because the definition of `NULL` is not the same in C and C++, so the answer will be different for the two languages, unlike the hybrid "C/C++". – Pete Becker Mar 25 '13 at 13:41
  • @LuchianGrigore I have tried such weird things, and they work. – CuriousSid Mar 25 '13 at 13:44
  • @Moderators: I think my question may be similar to other questions, but I want acknowledge the fact that NULL/false/\0/0x0 etc are similar and specifically want to know where NULL and false depart from others.....I got this warning message: "warning: converting to non-pointer type ‘int’ from NULL [-Wconversion-null]" while doing "int x = NULL;" signifying that there may be some points of difference. – CuriousSid Mar 25 '13 at 13:47
  • Just because they work, doesn't mean you should use them – Luchian Grigore Mar 25 '13 at 13:49
  • Also, as some answers pointed out, NULL doesn't have to be 0, so they're not equivalent. – Luchian Grigore Mar 25 '13 at 13:49
  • From the warning message I posted in a previous comment, I think NULL has a pointer type assigned to it. Using 'false' raised no such warning. – CuriousSid Mar 25 '13 at 13:56

6 Answers6

8

for some platforms NULL is not 0x0


From Is NULL in C required/defined to be zero? :

NULL is guaranteed to be zero, perhaps casted to (void *)1.

C99, §6.3.2.3, ¶3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.(55) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

And note 55 says:

55) The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant.

Notice that, because of how the rules for null pointers are formulated, the value you use to assign/compare null pointers is guaranteed to be zero, but the bit pattern actually stored inside the pointer can be any other thing (but AFAIK only few very esoteric platforms exploited this fact, and this should not be a problem anyway since to "see" the underlying bit pattern you should go into UB-land anyway).


So, as far as the standard is concerned, the two forms are equivalent (!ptr is equivalent to ptr==0 due to §6.5.3.3 ¶5, and ptr==0 is equivalent to ptr==NULL); if(!ptr) is also quite idiomatic.

That being said, I usually write explicitly if(ptr==NULL) instead of if(!ptr) to make it extra clear that I'm checking a pointer for nullity instead of some boolean value.


  1. Notice that in C++ the void * cast cannot be present due to the stricter implicit casting rules that would make the usage of such NULL cumbersome (you would have to explicitly convert it to the compared pointer's type every time).
Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • You can see the underlying bits just fine by looking at the pointer through a `char*` and reading out its bytes. What those bytes and bits [mean though is totally unspecified](http://stackoverflow.com/questions/14167455/is-it-possible-to-hash-pointers-in-portable-c03-code/14169897#14169897). – GManNickG Mar 25 '13 at 15:33
3

Well, NULL may not be zero, it just usually is. It's dependant on your platform - here are some nasty examples of non-zero NULL machines.

Joe
  • 7,378
  • 4
  • 37
  • 54
  • 2
    The null pointer's address may not have the numeric value zero; but a zero-valued integer constant will still be converted to a null pointer. – Mike Seymour Mar 25 '13 at 13:42
1

Generally in C++ you shouldn't use NULL. In C, NULL is a macro for (void*)0, whereas in C++ you should use 0 instead of NULL. But you can't use false instead of 0 for pointers! Apart from that, they are actually the same, sometimes causing confusion.
This is why in C++11 a nullptr was defined as use for pointers.

bash.d
  • 13,029
  • 3
  • 29
  • 42
1

NULL has got wide meaning in pointers. Most probably a pointer declared as NULL is something that cannot be referenced but just can be assigned something. whereas 0 or 0x0 can be the value of the pointer which can be both referenced and assigned.

meteors
  • 1,747
  • 3
  • 20
  • 40
0

from linux kernel stddef.h

#define NULL ((void *)0)

enum {
        false   = 0,
        true    = 1
};

So it is not exactly false

even though this post claims it is NULL , 0x0 and false are almost the same.

0x90
  • 39,472
  • 36
  • 165
  • 245
0

In C and historic C++, NULL has to be a zero-valued integer constant, typically 0. I think that C can include an explicit cast to void*, but C++ can't since that language doesn't allow implicit conversion from void* to other pointer types.

In modern C++, it could be either nullptr instead.

In either case, any zero-valued integer constant (including oddities such as '\0' and false) is convertible to a null pointer value.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644