Every way to own memory
Plain values, containers, unique_ptr, shared_ptr, weak_ptr and borrowing. Who ends each object's life, and how to choose.
Every object needs an answer to one question: who is responsible for ending its life? Work down this list and stop at the first answer that fits.
- A plain value, or a container of values (
Contact c;,std::vector<Contact>). No heap code of your own. This is the default, and it covers most programs. std::unique_ptr: one owner, when a plain value won't do. The object may be absent, must outlive a scope, or is one of several related types.std::shared_ptr: only when ownership is truly shared and you can't say in advance which owner finishes last.std::weak_ptr: to watch a shared object without keeping it alive (caches, back links, breaking a cycle).- A reference or a raw pointer: to use something someone else owns. Borrowers never delete.
Raw new/delete and C's malloc/free aren't on the list. They
belong inside the implementation of tools like these, and you should
recognize them in older code.
Owned for you
Section
Automatic lifetime on the stack
A local lives until its block ends, and is destroyed in reverse order. No code needed.
Section
A container owns its elements
std::vector, std::string and the other containers allocate and free their own heap memory.
A constructor acquires, the destructor releases, and the scope decides when.
Smart pointers
Section
std::unique_ptr, one owner
Can be moved but never copied. The same size as a raw pointer, and no extra cost at run time.
Shared ownership through a reference count, and the choosing-an-owner list above.
Classes that own things
Section
The Rule of Zero
Build a class from members that own themselves and it needs no destructor, copy or move code.
Section
The Rule of Five
The rare class that owns a raw resource writes all five special members.
The Old Way, to recognize
Section
Raw new and delete
Every new needs exactly one matching delete, and nothing may use the object afterwards. The compiler doesn't check it.
Section