Dynamic Memory Allocation in C++
Dynamic memory allocation creates objects whose lifetimes are independent of the scope that requested them.
Summary
Dynamic memory allocation creates objects whose lifetimes are independent of the scope that requested them. A new expression obtains storage, initializes an object, and returns a pointer. A delete expression destroys the object and releases its storage. Ordinary throwing new reports allocation failure by throwing std::bad_alloc.
| Allocation approach | Ownership and release |
|---|---|
| Single object using new | The responsible owner must use delete. |
| Array using new[] | The responsible owner must use delete[]. |
| RAII ownership | Smart pointers and containers release owned resources automatically. |
RAII ties resource ownership to an object's lifetime, supporting cleanup during normal returns and exception paths. std::make_unique creates exclusive smart-pointer ownership, std::make_shared creates shared ownership, and std::vector manages dynamic arrays.
A memory leak occurs when allocated storage remains unreleased after the program loses the means to free it. Deleting the same allocation repeatedly causes undefined behavior. After an object is deleted, pointers that still refer to it are dangling; accessing the destroyed object through them also causes undefined behavior.
Dynamic storage supports runtime-sized collections and objects that outlive their creating functions. However, unnecessary allocations add runtime overhead and can reduce cache locality compared with contiguous container storage.
Common Misconceptions: Leaving the requesting scope does not automatically destroy a manually allocated object. Setting one pointer to nullptr does not repair dangling aliases. Deleting a null pointer is safe, but mismatched or repeated deallocation is not.
🧠 Key Concepts
- Dynamic object lifetime
- Allocation pairing
- Memory leaks
- Dangling pointers
- Double deletion
- RAII
- Exclusive ownership
- Shared ownership
- Allocation failure
🧠 Quick Check
See what you remember from the summary.
What does the expression new int{42} do?
🧠 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.
Dynamic Memory Allocation in C++
📘 Overview Dynamic memory allocation creates objects whose lifetimes are controlled independently of the scope that requested them. C++ provides new and delete for explicit management, but standard containers and smart pointers usually provide safer ownership.
🧠 Key Idea Dynamically allocated objects remain alive until their storage is released by the responsible owner. Correct ownership prevents memory leaks, dangling pointers, and repeated deallocation.
⚔️ Core Details: - A new expression obtains storage, initializes an object, and returns a pointer: int* p = new int{42};. - A delete expression destroys an object and releases its storage: delete p;. - Array allocations require matching array deallocation: int* values = new int[count]{}; pairs with delete[] values;. - Ordinary throwing new reports allocation failure by throwing std::bad_alloc. - Deleting an object leaves other pointers to it dangling; accessing the destroyed object through them causes undefined behavior. - RAII ties resource ownership to an object's lifetime; std::unique_ptr, std::shared_ptr, and std::vector release owned resources automatically.
🎯 Why It Matters: - Runtime-sized storage supports collections whose sizes are unknown at compilation. - Explicit ownership allows objects to outlive the functions that create them without returning pointers to destroyed local objects. - Automatic resource management reduces leaks and use-after-free defects across normal returns and exception paths. - Unnecessary allocations add runtime overhead and can reduce cache locality compared with contiguous container storage.
🧠 Quick Recall: - Allocation pairing - Match new with delete and new[] with delete[]; mismatches cause undefined behavior. - Memory leak - Allocated storage remains unreleased after the program loses the means to free it. - Double deletion - Deallocating the same allocation more than once causes undefined behavior. - nullptr - The null pointer value; deleting a null pointer is safe, but nulling one pointer does not fix dangling aliases. - Ownership tools - std::make_unique creates exclusive smart-pointer ownership; std::make_shared creates shared ownership; std::vector manages dynamic arrays.
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.