NULL and Classic Pointer Mistakes
beginner13 min readLesson 40 of 148
The one safe invalid value, and the four bugs every C programmer meets.
NULL: the pointer that points at nothing โ on purpose
#include <stdio.h> // NULL lives in stdio.h and friends
int *p = NULL; // "points at nothing", a defined, testable value
if (p == NULL) { /* not aiming at any object */ }
Dereferencing NULL is undefined behavior โ typically a crash (on this course's sandbox, a segmentation fault). That crash is a gift: it is loud and immediate, unlike garbage pointers that silently corrupt.
Initialize pointers to NULL when you have nothing to point at yet.
Mistake 1: dangling pointer
int *p;
{
int local = 7;
p = &local;
} // local dies here
printf("%d\n", *p); // p still holds the dead address โ undefined behavior
The variable local lived in a stack frame that no longer exists. Pointers to
dead locals are dangling.
Mistake 2: dereferencing before assigning
int *p;
*p = 3; // p was never given an address โ undefined behavior
Mistake 3: returning the address of a local
int *make(void) {
int x = 5;
return &x; // x dies when the function returns โ dangling on arrival
}
Mistake 4: confusing address with value
int x = 1;
int *p = &x;
if (p == &x) { } // comparing addresses โ true
if (*p == x) { } // comparing values โ true
if (p == x) { } // comparing address to int โ nonsense (and a warning)
The compiler's warnings are your first defense: read them.