Memory Management Terminology Reference

This reference provides an overview of memory management terminology you'll encounter in this chapter. Use it as a quick lookup guide.

Memory Regions

Term Definition Example
Stack Automatic memory region for local variables, managed by compiler int x{5}; in a function
Heap Dynamic memory region for manually allocated objects new int{5}
Free Store Another term for heap, emphasizing C++ dynamic allocation Memory obtained via new/delete
Static Storage Memory region for global and static variables static int count{0};
Automatic Storage Memory automatically allocated/deallocated for local variables Function parameters and local variables

Dynamic Memory Allocation

Term Definition Example
new Operator Allocates memory on heap and calls constructor int* ptr{new int{42}};
delete Operator Calls destructor and deallocates heap memory delete ptr;
new[] Operator Allocates array of objects on heap int* arr{new int[10]};
delete[] Operator Deallocates array allocated with new[] delete[] arr;
Placement new Constructs object at pre-allocated memory address new (buffer) MyClass{};
nothrow new Allocation that returns nullptr on failure instead of throwing int* ptr{new (std::nothrow) int[1000000]};

Smart Pointers

Term Definition Example
unique_ptr Smart pointer with exclusive ownership, non-copyable std::unique_ptr<int> ptr{std::make_unique<int>(42)};
shared_ptr Smart pointer with shared ownership using reference counting std::shared_ptr<int> ptr{std::make_shared<int>(42)};
weak_ptr Non-owning reference to shared_ptr to break circular references std::weak_ptr<int> wptr{sptr};
make_unique Factory function to create unique_ptr safely auto ptr{std::make_unique<int>(42)};
make_shared Factory function to create shared_ptr efficiently auto ptr{std::make_shared<int>(42)};
get() Returns raw pointer without transferring ownership int* raw{ptr.get()};
reset() Releases current object and optionally takes new ownership ptr.reset(new int{10});
release() Releases ownership without deleting, returns raw pointer int* raw{ptr.release()};

Memory Issues

Term Definition Example
Memory Leak Allocated memory that's never deallocated new int{5}; without corresponding delete
Dangling Pointer Pointer to memory that has been deallocated Using pointer after delete
Double Delete Deleting same memory twice, causing undefined behavior delete ptr; delete ptr;
Use After Free Accessing memory after it has been deallocated Dereferencing deleted pointer
Buffer Overflow Writing beyond allocated memory boundaries arr[10] = 5; when array has size 10
Null Pointer Dereference Attempting to access memory through null pointer *ptr when ptr == nullptr

Ownership and Resource Management

Term Definition Example
RAII Resource Acquisition Is Initialization, acquiring resources in constructor Smart pointers, file handles
Ownership Which part of code is responsible for resource lifetime Unique ownership vs shared ownership
Transfer of Ownership Moving ownership from one entity to another std::move(unique_ptr)
Shared Ownership Multiple entities share responsibility for resource Multiple shared_ptr to same object
Reference Counting Tracking number of references to determine when to delete shared_ptr use count
Resource Lifecycle Complete lifetime from acquisition to release Constructor to destructor

Allocation Strategies

Term Definition Example
Custom Allocator User-defined memory allocation strategy std::vector<int, MyAllocator<int>>
Memory Pool Pre-allocated block of memory for fast allocation Object pool pattern
Stack Allocation Allocation on stack for performance Local variables
Heap Allocation Allocation on heap for flexibility and size Dynamic arrays
Aligned Allocation Allocating memory with specific alignment requirements aligned_alloc, std::aligned_storage

Reference Counting Concepts

Term Definition Example
Strong Reference Reference that keeps object alive shared_ptr
Weak Reference Reference that doesn't prevent deletion weak_ptr
Reference Count Number of strong references to an object sptr.use_count()
Circular Reference Two objects holding strong references to each other Causes memory leak without weak_ptr
lock() Converts weak_ptr to shared_ptr if object still exists if (auto sptr{wptr.lock()}) { }
expired() Checks if weak_ptr points to deleted object if (wptr.expired()) { }

Memory Safety Patterns

Term Definition Example
Defensive Copy Creating copy instead of sharing pointer Deep copy in copy constructor
Null Check Verifying pointer is not null before use if (ptr != nullptr) { *ptr = 5; }
Smart Pointer Cast Converting between smart pointer types safely std::static_pointer_cast, std::dynamic_pointer_cast
Custom Deleter Custom cleanup function for smart pointers shared_ptr<FILE>(fopen("f.txt", "r"), fclose)
RAII Wrapper Class that manages resource lifetime Smart pointers, lock guards