Common Memory Errors in C++
Safe memory use in C++ requires initialized values, valid access bounds, live objects, and correct ownership.
Summary
Safe memory use in C++ requires initialized values, valid access bounds, live objects, and correct ownership. Invalid memory operations often cause undefined behavior: the C++ standard imposes no requirements on subsequent program behavior. A program may appear to work while corrupting state or creating security vulnerabilities.
| Error category | Cause | Consequence |
|---|---|---|
| Invalid reads or access | Uninitialized scalars, unchecked out-of-bounds access, or dangling pointers | Unreliable values, undefined or erroneous behavior |
| Invalid deallocation | Releasing storage twice, deleting a non-owned address, or mismatching allocation and deallocation | Undefined behavior |
| Memory leaks | Losing ownership without releasing allocated storage | Increasing memory consumption, especially in long-running applications |
Dangling pointers and references can result from deallocation, scope exit, or container invalidation. Use-after-free specifically involves access after storage has been deallocated.
Pair new with delete and new[] with delete[]; do not mix these with malloc and free. Prefer automatic storage, standard containers such as std::vector, and RAII-based ownership such as std::unique_ptr. RAII ties cleanup to the owning object's lifetime through its destructor. Ownership cycles involving std::shared_ptr can still leak.
AddressSanitizer detects many invalid accesses and deallocation errors. MemorySanitizer detects many uninitialized reads.
Common Misconceptions Uninitialized local scalars do not have reliable default values. Undefined behavior does not guarantee a crash. Deleting a null pointer is safe, but dereferencing it is invalid.
🧠 Key Concepts
- Uninitialized Reads
- Out-of-Bounds Access
- Dangling Pointers
- Invalid Deallocation
- Memory Leaks
- Undefined Behavior
- RAII
- Allocation Pairing
- Null Pointers
- Memory Sanitizers
🧠 Quick Check
See what you remember from the summary.
What best describes accessing an array element outside its valid range using an unchecked operation?
🧠 Flashcards Preview
Tap a card to reveal the definition.
Ready to quiz yourself?
Test what you remember with a full practice quiz on this note. Create a free account and start in seconds.
Full Notes
Read the original note content before deciding whether to save or study from it.
Common Memory Errors in C++
📘 Overview C++ memory errors arise from invalid access, incorrect object lifetimes, or mismanaged resource ownership. Many cause undefined behavior rather than a predictable crash; memory leaks instead leave allocated storage unreleased.
🧠 Key Idea Safe memory use requires initialized values, valid bounds, live objects, and correct ownership. Prefer automatic storage, standard containers, and RAII-based smart pointers over manually managed allocations.
⚔️ Core Details: - Uninitialized reads: Reading an uninitialized local scalar, such as an int or pointer, does not produce a reliable default value and can cause undefined or erroneous behavior. - Out-of-bounds access: Accessing an array or container element outside its valid range causes undefined behavior with unchecked operations such as operator[]. - Dangling access: Using a pointer or reference after the referenced object's lifetime ends is invalid, including after deallocation, scope exit, or container invalidation. - Invalid deallocation: Deleting an allocation twice, deleting a non-owned address, or mismatching new with delete[] or new[] with delete causes undefined behavior. - Memory leak: Losing ownership of dynamically allocated storage without releasing it leaves that storage allocated. - RAII: Resource Acquisition Is Initialization ties resource cleanup to an owning object's lifetime through its destructor.
🎯 Why It Matters: - Invalid memory access can corrupt program state or create exploitable vulnerabilities even when the program appears to run correctly. - Leaks can steadily increase memory consumption in long-running applications such as servers. - std::vector and std::unique_ptr reduce manual memory management; shared ownership cycles involving std::shared_ptr may still leak. - AddressSanitizer detects many invalid accesses and deallocation errors; MemorySanitizer detects many uninitialized reads.
🧠 Quick Recall: - Undefined behavior - The C++ standard imposes no requirements on the program's behavior after the relevant invalid operation. - Allocation pairing - Pair new with delete and new[] with delete[]; never mix either pair with malloc and free. - Use-after-free - Access to an object through a pointer or reference after its storage has been deallocated. - Double free - Attempting to release the same allocation more than once. - Null pointer - A pointer that designates no object; deleting it is safe, but dereferencing it is invalid.
More ways to study when you copy this note
Copy this note into your library to unlock focused practice sessions and long-term review.
Answer all questions first, then see feedback at the end — the way real exams work.
Focuses each session on what you got wrong, not what you already know.
Full timed exam with all questions, no pausing, and results at the end. Built for board exam prep.
Part of 🗄️ BS Information Systems — 1st Year
Browse this Review Set →More Computer Engineering notes
See all →More in Programming Fundamentals
See all →More from NoteLib
Browse NoteLib's public notes →Copy this note to your library and get the full Study Pack instantly — summary, key concepts, and practice quiz included.