Is NULL / false in C and C ++ something more than 0x0 / 0b0 / '\ 0' / 0

As mentioned in the question, I used NULL and false (in C ++) interchangeably with 0 or 0x0 and so on. I was curious to know if they have any special meaning except synonyms 0.

+4
source share
6 answers

for some platforms, NULL not 0x0


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

NULL guaranteed to be zero, possibly being cast to (void *) 1 .

C99, Β§6.3.2.3, ΒΆ3

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

And note 55 says:

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

Please note that due to the way 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 (but AFAIK only few very esoteric platforms have used this fact, and this should not be a problem, since you should go to UB -land anyway to β€œsee” the basic structure of bits).


So, as for the standard, the two forms are equivalent ( !ptr equivalent to ptr==0 due to 6.5.3.3 ΒΆ5, and ptr==0 equivalent to ptr==NULL ); if(!ptr) also pretty idiomatic.

As I said, I usually write if(ptr==NULL) instead of if(!ptr) to make it clear that I am checking for a pointer to nullity instead of some boolean value.


  • Note that in C ++, void * casting cannot be present due to stricter implicit casting rules that will make using such NULL cumbersome (you will have to explicitly convert it to a comparable pointer type each time).
+5
source

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

+3
source

Normally in C++ you should not use NULL . In C NULL is the macro for (void*)0 , while in C++ you should use 0 instead of NULL . But you cannot use false instead of 0 for pointers! In addition, they are actually the same, which sometimes causes confusion.
This is why in C++11 a nullptr was defined as using pointers.

+1
source

NULL has a wide meaning in pointers. Most likely, a pointer declared as NULL is something that cannot be referenced, but something can be assigned to it. while 0 or 0x0 can be the value of a pointer that can be referenced and assigned.

+1
source

from linux kernel stddef.h

 #define NULL ((void *)0) enum { false = 0, true = 1 }; 

So this is not completely false

although this one claims to be NULL , 0x0 and false almost the same.

0
source

In C and historical C ++, NULL should be a null integer constant, usually 0 . I think that C may include explicit casting to void* , but C ++ cannot, because this language does not allow implicit conversion from void* to other types of pointers.

In modern C ++, it can be like nullptr .

In any case, any null integer constant (including oddities such as '\0' and false ) can be converted to a null pointer value.

0
source

All Articles