Is NULL in C mandatory / defined to be zero?

In my GCC test programs, the NULL value is zero, but wikipedia says that NULL needs to point to non-addressable memory.

Are any compilers making NULL non-zero? I'm curious if if (ptr == NULL) is better than if (!ptr) .

+49
c null
Nov 27 '11 at 19:48
source share
4 answers

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 null pointer constant is converted to a pointer type, 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 several very esoteric platforms have used this fact, and this should by no means be a problem, since you must enter UB to “see” the basic bit diagram - anyway).




So, with regard to the standard, 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.

In doing so, I usually write explicitly 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.




  • Please note that in C ++, the void * butt cannot be present due to more stringent implicit casting rules that will make using such NULL cumbersome (you will have to explicitly convert it to a comparable pointer type each time).
+63
Nov 27 '11 at 19:52
source share

From the standard language:

6.3.2.3 Pointers
...
3 An integer constant expression with a value of 0 or such an expression cast for type void * is called a constant constant null . 55) If the constant of the null pointer is converted to a type of pointer, the resulting pointer, called the null pointer , is guaranteed to compare unequal to the pointer to any object or function.
...
55) The NULL macro NULL defined in <stddef.h> (and other headers) as a null pointer constant; see 7.17.

Given this language, a NULL macro should be evaluated using a zero-valued expression (either an undecorated literal 0 , an expression like (void *) 0 , or another macro or expression that ultimately evaluates to 0). The expressions ptr == NULL and !ptr must be equivalent. The second form tends to be a more idiomatic C code.

Note that the value of a null pointer does not have to be 0. The main implementation can use any value that it wants to represent a null pointer. However, for your source code, a pointer expression with a null value is a null pointer.

+11
Nov 27 '11 at 20:05
source share

In practice, the same, but NULL is different from zero. Since zero means that the value and NULL means that they are not. So, theoretically they are different, NULL has a different meaning, and in some cases this difference should be useful.

+3
Nov 28 2018-11-11T00:
source share

in practice, no! ptr is correct

+2
Nov 27 '11 at 19:49
source share



All Articles