Reading a Pointer Value After free

suggest change

After 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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents