Pointer
Pitfalls
Master the most dangerous bugs in C++: dangling pointers, uninitialized pointers, and memory mismanagement. Learn to spot, prevent, and fix them.
What Is a Pointer?
A pointer is a variable that stores the memory address of another variable. Instead of holding data directly, it holds a reference to where that data lives in memory.
Why Pointers Go Wrong
Pointers are powerful but unforgiving. C++ gives you direct access to memory, but the language does not automatically verify that a pointer is valid before you use it. This creates several categories of bugs:
nullptr but the program tries to read or write through it.The Pointer Lifecycle
Uninitialized Pointers
When you declare a pointer without assigning it a value, it contains whatever garbage data happened to be in that memory location. Dereferencing it is undefined behavior.
Dangling Pointers
A dangling pointer points to memory that has been freed or has gone out of scope. The address is still stored, but the data there is no longer yours.
Three Causes of Dangling
delete ptr, the pointer still holds the old address. Any dereference is undefined behavior.&localVar. Once the function ends, that stack frame is reclaimed.Uninitialized vs. Dangling
| Uninitialized | Dangling | |
|---|---|---|
| When | At declaration | After delete or scope exit |
| Address held | Random garbage | Previously valid address |
| Was it ever valid? | Never | Once, but no longer |
| Detection | Compiler warnings | Hard: may work sometimes |
| Fix | Always initialize | Set to nullptr after delete |
The Golden Rules
nullptr or a valid address at declaration.ptr = nullptr; after every delete ptr;.if (ptr != nullptr).&localVar from a function.std::unique_ptr or std::shared_ptr to automate lifetime management.Pointer Safety Checklist
Before using any pointer, walk through this checklist:
nullptr?nullptr before dereferencing?delete, is it set to nullptr?Example 1: Uninitialized Pointer
Example 2: Dangling After Delete
Example 3: Returning a Local Address
Null Pointer Dereference
Setting a pointer to nullptr is a safety net, not a fix by itself. You must check before you dereference.
Common Mistakes
delete twice on the same pointer without setting it to nullptr in between. Causes heap corruption.delete instead of delete[] on array allocations (or vice versa). Undefined behavior.Pitfall Summary
| Pitfall | Symptom | Prevention |
|---|---|---|
| Uninitialized | Random crash or corruption | Initialize to nullptr or valid address |
| Dangling | Intermittent bugs, data corruption | Set to nullptr after delete |
| Null deref | Immediate crash (segfault) | Check != nullptr before use |
| Double delete | Heap corruption | Null after delete, use smart pointers |
| Returning local addr | Dangling after function returns | Use heap allocation or return by value |
Question 1 of 5
What is an uninitialized pointer?
Question 2 of 5
Code snippet:
int* p = new int(10); delete p; *p = 20;
What type of pointer error does this code demonstrate?
delete p, the pointer still holds the old address but the memory is freed. Dereferencing it (*p = 20) is a dangling pointer error: use after free.Question 3 of 5
Which line should be added immediately after delete ptr; to prevent a dangling pointer?
delete ensures that any subsequent dereference attempt will cause a predictable crash rather than silent corruption. Option D would be a double delete, which is itself undefined behavior.Question 4 of 5
Code snippet:
int* getVal() {
int x = 5;
return &x;
}
int* p = getVal();
cout << *p;
Why is the final line of this code undefined behavior?
getVal() ends, its local variable x is destroyed (its stack frame is popped). The returned address now refers to freed stack memory, making p a dangling pointer.Question 5 of 5
Which modern C++ feature best prevents dangling pointers and memory leaks by automatically managing object lifetimes?
delete when the pointer goes out of scope, eliminating the chance of forgetting to free memory or using it after free. They are the modern C++ solution to virtually all raw pointer pitfalls.