Dangling Pointer
How Dangling Pointers Occur
Example
#include <iostream>
int* dangerousFunction() {
int x = 10;
return &x; // Returning the address of a local variable
}
int main() {
int* ptr = dangerousFunction();
std::cout << *ptr << std::endl; // Undefined behavior!
return 0;
}Consequences of Dangling Pointers
Preventing Dangling Pointers
Last updated