Is NULL always false?

2020-01-24 12:04发布

Is it safe to assume that NULL always translates to false in C?

void *somePtr = NULL;

if (!somePtr) {
  /* This will always be executed? */
}

Or should an explicit check against the value of NULL be made?

标签: c null pointers
13条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-01-24 12:45

Yes (at least for any standards compliant C compiler!)

From the comp.lang.c FAQ:

Q: Is the abbreviated pointer comparison ``if(p)'' to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?

A: It is always valid.

查看更多
▲ chillily
3楼-- · 2020-01-24 12:46

Yes, if(!p) is valid and guaranteed to work.

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 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.

https://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p3

This means that (void*)0 is a null pointer. It also means that if p is a pointer, then p==0 is equivalent to p==(void*)0.

It also means that if p is a not a null pointer, then p==(void*)0 will evaluate to 0.

So far, so good.

Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.

http://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p4

Note that "Any two null pointers shall compare equal." This means that if p is a null pointer, then p==0 will evaluate to true, because 0 will be promoted to (void*)0 which is a null pointer. It also mean that no non-null pointer can be equal to a null pointer.

Let's have a look at the negation operator.

The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

http://port70.net/~nsz/c/c11/n1570.html#6.5.3.3p5

This tells us that !p is the same as p==0 by definition, which is the same as p==(void*)0 as mentioned above.

And taking the fact that all null pointers are equal into consideration, this means that p==(void*)0 can only evaluate to true if p is a null pointer and and only false if p is not a null pointer.

So yes, if(!p) is a perfectly safe way to check if p is a null pointer or not.

查看更多
唯我独甜
4楼-- · 2020-01-24 12:49

NULL is defined as a constant pointer that is guaranteed to point to a useless/non-existent place in memory. Most implementations of NULL are ((void *)0) but it is not mandatory that this is so.

查看更多
放荡不羁爱自由
5楼-- · 2020-01-24 12:51

Yes it:

C standard 6.3.2.3

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.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 6.3.2.6

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

-

When any scalar value is converted to _Bool, the result is 0 if the value compares equal to 0; otherwise, the result is 1

查看更多
唯我独甜
6楼-- · 2020-01-24 12:53

It's never safe to assume anything.

An explicit check is also more clear about what you're testing.

查看更多
forever°为你锁心
7楼-- · 2020-01-24 12:53


What is NULL?

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


What is the value of the null pointer constant?

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

Example Definition:

#define NULL ((void*)0)


Evaluating if(NULL)

if ( expression ) statement

if ( expression ) statement else statement

In both forms, the first substatement is executed if the expression compares unequal to 0. In the else form, the second substatement is executed if the expression compares equal §6.8.4.1 Language 133 ISO/IEC 9899:TC3 Committee Draft — Septermber 7, 2007 WG14/N1256 to 0. If the first substatement is reached via a label, the second substatement is not executed.


So yes if the compiler is compliant with ISO C99 you can assume that the statement below will always execute.

if (!NULL) { statement; } 


Above quotations are from ISO/IEC 9899:1999 (C99). You can read it here.

查看更多
登录 后发表回答