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?
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?
*NULL always targets to 0x00L. You can consider that false, but to be sure always do an explicit check.
NULL is just a preprocessor definition. It's in stdio.h. Typically, only an insane person would redefine it, but it's possible. An example:
This code will print "NULL is true". Try it if you don't believe me. Your compiler might not even warn you that you're doing something weird.
The 'C' language dates from an era where (void*)0 could actually be a valid pointer. It is not that long ago, the 8080 and Z80 microprocessors had an interrupt vector at address 0. Faced with such architecture choices, it couldn't do anything but let a header file declare the value of NULL. There were some compilers out there, now long forgotten, where NULL was not equal to (void*)0 (0xffff was the next alternative), thus giving your if() statement undefined behavior.
C++ mercifully put an end to this, a null pointer is assignable from and testable against 0.
Yes, mostly.
First off, NULL is a typedef. I could royally screw you over by saying in a previously included header
This might not make a lot of sense, but since when has other people's code ever made sense? :)
Also, while it's probably syntactically safe, it's not semantically correct. NULL means "nothing", neither true or false or a boolean value or int or string. It means "a symbol for nothing". So testing for NULL is more like a philisophical issue: If a tree falls in the forest, and
if(listener)
, does it make a sound?Do everyone a favor and be clear about testing against NULL.
My copy of ISO/IEC 9899:TC3 (Committee Draft — Septermber 7, 2007) says:
So far,
ptr!=0
is true (1) for every non-nullptr
, but it's still open, how two null pointers compare.Hence,
ptr==0
is 1 (andptr!=0
is 0), if and only ifptr
is a null pointer.So the same holds for
!ptr
.Note, that a scalar type is an arithmetic type or a pointer type (see "6.2.5 Types", clause 21). Putting it together, we have:
if (ptr)
succeeds ⇔ptr!=0
is 1 ⇔ptr
is not a null pointer.if (!ptr)
succeeds ⇔ptr==0
is 1 ⇔ptr
is a null pointer.According to me, it's not always safe to assume that. Since, depending on which headers have been included in a program, it can have been redefined. But according to the standard, the null pointer constant is guaranteed not to point to any real object and has a type
void *
.In our case, the declaration
void *somePtr = NULL
could also bevoid *somePtr = 0
with0
as null pointer.That simply means, evaluating
*somePtr
at that specific point could result to false.Another reference can be https://www.gnu.org/software/libc/manual/pdf/libc.pdf at page 944 A.3 about NULL Pointer Constant