Skip to content

Latest commit

 

History

History
36 lines (23 loc) · 1.75 KB

File metadata and controls

36 lines (23 loc) · 1.75 KB

Smart pointers

Smart pointers are special pointers that automatically free the memory they manage under certain conditions. By using smart pointers, one can avoid manually using new and delete in most cases, leading to a much safer memory management. The two most useful types of smart pointers are unique pointers and shared pointers.

Unique pointers

Unique pointers, (std::unique_ptr<T>) are very lightweight objects that:

  • encapsulate a pointer;
  • cannot be copied;
  • free the memory when they are destroyed.

A unique pointer means "I am the only one responsible for managing this piece of memory". Normal pointers can be created from a unique pointer but should never be freed.

Compared to a normal pointer, std::unique_ptr<T> has very little overhead (in particular, no overhead on dereferencing). Details.

Motivating example: motiv-unique.cpp

Minimal working example: unique_ptr.cpp

Shared pointers

Shared pointers (std::shared_ptr<T>) are copyable smart pointers that free the managed memory when the last copy of the pointer is destroyed. They can be useful when managing shared resources or DAG-like structures.

Be careful though, they are more costly that unique pointers, in particular at creation and destruction. Details. Also, cycles of shared pointers can keep each other alive and lead to memory leaks.

Example: shared.cpp