Values & References12 / 18
Up next

Avoid dangling pointers

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int *p = malloc(sizeof *p);
    *p = 5;
    printf("%d\n", *p);
    free(p);
    p = nullptr;
    printf("%d\n", p == nullptr);
    return 0;
}
What you're solving

clear a pointer after freeing it