Dangling and NULL Pointers
A NULL pointer points nowhere; a dangling pointer points to memory that is no longer valid.
Intermediate
int *p = NULL;
if (p != NULL) *p = 5; // always check before dereferencing
int *make(void) {
int local = 5;
return &local; // BUG: dangling — local dies when the function returns
}
free(buf);
buf = NULL; // avoid a dangling pointer after free
Dereferencing NULL usually crashes; dereferencing a dangling pointer silently corrupts data — much harder to debug. Tools like AddressSanitizer and Valgrind find these bugs.
Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.
Discussion (0)
Sign in or join free to ask questions and discuss this topic.