Skip to content
UPEEC

Memory and Resource Management in C++

Ownership, lifetimes, allocation and move semantics. Smart pointers and RAII have their own pages earlier in this subject.

Intermediate
Comments 0

This is an overview — pick a sub-topic below.

Sub-topics (14)

Pointers in C++

Raw pointers still exist, but in modern C++ they mean "non-owning observer".

Easy♥ 0🔖 0💬 0

unique_ptr

Sole ownership of a heap object; deleted automatically, movable but not copyable.

Easy♥ 0🔖 0💬 0

shared_ptr

Shared ownership with reference counting; the object dies when the last owner goes.

Intermediate♥ 0🔖 0💬 0

weak_ptr

A non-owning reference to a `shared_ptr` object that can check whether it's still alive.

Intermediate♥ 0🔖 0💬 0

new and delete

Allocate and construct / destroy and deallocate objects.

Easy♥ 0🔖 0💬 0

Memory Alignment in C++

`alignof`, `alignas` and `std::aligned_alloc` control object alignment.

Intermediate♥ 0🔖 0💬 0

Object Lifetime

When an object begins and ends — and rules about using it outside that window.

Advanced♥ 0🔖 0💬 0

Placement new

Construct an object in memory you already have: `new (buffer) T(args)`.

Advanced♥ 0🔖 0💬 0

Allocators

Objects that supply memory to containers.

Advanced♥ 0🔖 0💬 0

Custom Memory Pools

Pre-allocate blocks of fixed-size objects for fast, fragmentation-free allocation.

Advanced♥ 0🔖 0💬 0

Move Semantics

Transfer ownership of resources from temporaries instead of copying them.

Intermediate♥ 0🔖 0💬 0

Perfect Forwarding

Pass arguments through a template keeping their value category, using `T&&` + `std::forward<T>`.

Advanced♥ 0🔖 0💬 0

Rule of Zero, Three and Five

If a class manages a resource, define all special members (Five); better, let members manage it and define none (Zero).

Intermediate♥ 0🔖 0💬 0

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.