Dangling pointer là con trỏ trỏ tới vùng nhớ đã bị giải phóng — đọc/ghi vào đó là undefined behavior (crash hoặc corrupt dữ liệu ngầm).
cpp
int* p = new int(10);
delete p;
p = nullptr; // gán null ngay sau delete — an toàn
if (p) { *p = 20; } // điều kiện false, không vàoCách tránh tốt hơn: dùng smart pointer (std::unique_ptr, std::shared_ptr) — tự giải phóng khi ra scope, không bao giờ để con trỏ trỏ lung tung.
A dangling pointer points to memory that has already been freed — reading or writing through it is undefined behavior (crash or silent data corruption).
cpp
int* p = new int(10);
delete p;
p = nullptr; // set to null immediately after delete — safe
if (p) { *p = 20; } // condition is false, never entersBetter approach: use smart pointers (std::unique_ptr, std::shared_ptr) — they self-destruct when leaving scope, preventing dangling entirely.