Aussie AI
Smart Pointers
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
Smart Pointers
Smart pointers are a programming idiom to make C++ pointers safer. They are not a speed optimization, and in fact, they are a wrapper that adds extra logic around the use of a raw pointer, and will be marginally slower. However, they avoid many C++ pointer pitfalls, thereby improving reliability, and will reduce total allocated memory usage by avoiding memory leaks. There may even be an indirect benefit to execution speed if overall memory management is improved.
Programmers have been defining their own smart pointer wrapper classes for decades,
but there is now standard support for the idea in the C++ library.
In the typical idiom, a smart pointer tracks the creation and destruction of the object
it points to, which ensures that the destructor is called.
This helps avoid “memory leaks” in standard C++ pointers where an object is allocated
with “new
”, but is never deallocated by “delete
”.
The C++ standard libraries have various templates to support smart pointers, mostly since C++11, so they are longstanding features.
std::shared_ptr
std::unique_ptr
std::weak_ptr
std::shared_ptr
is a reference-counted shared pointer implementation.
The idea is that it tracks the total number of pointers to an object,
and then automatically destroys the object whenever there's no more pointers to it.
This occurs when the last of the “shared_ptr
” objects is itself destroyed,
and then the reference count for the underlying object is zero.
std::unique_ptr
is a one-to-one mapping of a smart pointer to an object.
Whenever the unique_ptr
object is destroyed (e.g. goes out of scope
as a local variable), then both the smart pointer and its underlying object
are destroyed or otherwise cleaned up.
The unique_ptr
object can refer to a single object allocated by “new
”
or a single array-of-objects allocated by the “new[]
” operator.
std::weak_ptr
is a less commonly used type that has relevance to std::shared_ptr
in some complicated scenarios.
Usually, you should choose either of std::unique_ptr
or std::shared_ptr
,
depending on how many pointers will point to the underlying object.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |