1. Intro
A dangling pointer is a pointer that still holds the address of memory that is no longer valid — because the memory has been freed (deleted) or because the variable it pointed to has gone out of scope. The pointer itself still "looks" valid (it holds some address), but using it to read or write data is undefined behavior, since that memory may have been reused for something else entirely.
Cricket analogy: A dangling pointer is like a fan still holding an entry pass to a stadium seat that's since been demolished and rebuilt as a parking lot — the pass looks valid, but using it now leads to unpredictable trouble.
2. Syntax
int* p = new int(10);
delete p; // memory freed, but p still holds the old address
// p is now a dangling pointer
*p = 5; // undefined behavior - accessing freed memory
p = nullptr; // safe: neutralize the dangling pointer3. Explanation
A pointer becomes dangling in a few common situations: (1) after calling delete on the memory it points to, without also resetting the pointer; (2) when it points to a local (stack) variable inside a function, and that function returns, destroying the variable while the pointer that referenced it still exists elsewhere; and (3) when multiple pointers point to the same heap block and one of them deletes it, leaving the others dangling. The danger is that a dangling pointer's address is not automatically invalidated or marked — the pointer still contains a real-looking address, and dereferencing it (*p) does not necessarily crash immediately. Instead it produces undefined behavior: it might silently read/write garbage data, corrupt unrelated memory that has since been reused, or crash unpredictably — bugs from dangling pointers are notoriously hard to track down because the failure can appear far away from the actual mistake. The standard defense is discipline: immediately set a pointer to nullptr after delete-ing it, never return the address of a local stack variable from a function, and avoid having multiple raw pointers independently own and free the same memory. Checking if (p != nullptr) before use is only effective if the pointer was properly nulled out after deletion — it does nothing to protect against a dangling pointer that was never reset.
Cricket analogy: A pointer dangles like a scorer still tracking 'Player 7' after that player retired and the jersey number was reissued to a rookie — the fix is to immediately retire the number (set to nullptr) after departure, never reference a substitute player who's left the ground, and avoid two scorers independently tracking and 'releasing' the same jersey number, since the resulting confusion may not show up until several overs later.
A dangling pointer points to memory that has already been freed or gone out of scope. Dereferencing it is undefined behavior — it may crash, corrupt unrelated data, or appear to work by accident. Always set a pointer to nullptr immediately after delete-ing it.
4. Example
#include <iostream>
using namespace std;
int main() {
int* p = new int(10);
cout << "Before delete, *p = " << *p << endl;
delete p; // memory is freed; p is now dangling
p = nullptr; // best practice: neutralize the dangling pointer
if (p == nullptr) {
cout << "p has been safely nulled out after delete." << endl;
}
// *p = 20; // would be undefined behavior if p were not nulled
return 0;
}5. Output
Before delete, *p = 10
p has been safely nulled out after delete.6. Key Takeaways
- A dangling pointer still holds the address of memory that has been freed or gone out of scope.
- Common causes: forgetting to reset a pointer after
delete, returning the address of a local variable, or multiple pointers deleting the same memory. - Dereferencing a dangling pointer is undefined behavior — it may crash, corrupt data, or appear to work, making it hard to debug.
- Best practice: set a pointer to
nullptrimmediately afterdelete, and never return addresses of local stack variables from functions.
Practice what you learned
1. What is a dangling pointer?
2. Which of these creates a dangling pointer?
3. What happens if you dereference a dangling pointer?
4. What is the recommended way to avoid problems from a dangling pointer after `delete`?
5. Which scenario also produces a dangling pointer, besides deleting heap memory?
Was this page helpful?
You May Also Like
Dynamic Memory Allocation in C++
Learn how `new` and `delete` allocate and free heap memory at runtime, and why forgetting to `delete` causes a memory leak.
Null Pointers in C++
Learn what a null pointer is, why modern C++ prefers `nullptr` over the old `NULL` macro, and why dereferencing one causes undefined behavior.
Pointers in C++
Learn what a pointer is, how to declare one, and how the `&` and `*` operators let you access and manipulate memory addresses directly.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics