Dangling Pointer
Dangling pointers arise when an object is deleted or deallocated but a pointer still references that memory location, leading to undefined behavior, crashes, or security vulnerabilities. The article explains causes, consequences, and common prevention methods like setting pointers to NULL after deallocation.
Background
- A **dangling pointer** is a programming bug that occurs when a piece of memory is freed (released back to the system) but the program still holds a reference (pointer) to that now-invalid location.
- Using a dangling pointer can cause crashes, data corruption, or security vulnerabilities — the program might read or write garbage, or an attacker could place malicious data in the freed memory.
- The term comes from C and C++, where manual memory management is common. Languages like Java, Python, or Rust prevent dangling pointers via garbage collection or ownership rules.
- Related concepts: **null pointer** (points to address 0, usually safe to check), **buffer overflow**, **use-after-free** (a specific type of dangling-pointer exploit often used in hacking).