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?
It's never safe to assume anything.
An explicit check is also more clear about what you're testing.
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.
*NULL always targets to 0x00L. you can consider that false, but to be sure always do a explicit check.
Yes. NULL evaluates to false, since C considers any non-zero value true and any zero value false. NULL is essentially the zero
address and is treated as such in comparisons, and I believe would be promoted to an int for the boolean check. I would expect that your code is readable to anyone familiar with C although I would probably make the check explicit.
In C and C++ programming, two null pointers are guaranteed to compare equal; ANSI C guarantees that any null pointer will be equal to 0 in a comparison with an integer type; furthermore the macro NULL is defined as a null pointer constant, that is value 0 (either as an integer type or converted to a pointer to void), so a null pointer will compare equal to NULL.
Ref: http://en.wikipedia.org/wiki/Null_pointer#The_null_pointer
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 guaranteed to be 0.
Yes, mostly.
First off, NULL is a typedef. I could royally screw you over by saying in a previously included header
#define NULL 1
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.
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.
I simply refer you to Question 5.3 of the C-FAQ. It answers this exact question.
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:
#include <stdio.h>
#ifdef NULL
#undef NULL
#define NULL 1
#endif
void main()
{
if (NULL)
printf("NULL is true\n");
else
printf("NULL is false\n");
}
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.