Reading a Pointer Value After free
suggest changeAfter free(p), the allocated object's lifetime ends and the value of p becomes indeterminate. Evaluating that indeterminate pointer value—even for a comparison rather than a dereference—can have undefined behavior.
char *p = malloc(5);
if (p != NULL) {
free(p);
p = NULL;
}
Assigning NULL immediately after free makes that particular pointer safe to inspect and helps prevent accidental reuse. It does not change any copies of the old pointer; those copies remain invalid.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents