Pointers & Memory · advanced · ~8 min
Recognize pointers whose target storage has gone away.
A dangling pointer is one whose target has been deallocated or has gone out of scope. Two common sources:
Both lead to use-after-free style bugs. The compiler can sometimes warn (-Wreturn-local-addr); AddressSanitizer catches the rest at runtime.
// BAD
int *bad(void) {
int x = 42;
return &x; // x dies at return
}
T inside a for loop body.