Defined in header , which is part of the dynamic memory management library.
Detail in could be found here.
There are several parts on dynamic memory management: ***Smart pointers***, Allocators, Memory resources, Uninitialized storage, Constrained uninitialized memory algorithms, Garbage collector support, Miscellaneous.
Smart pointers enable automatic, exception-safe, object lifetime management. Defined in header .
-
unique_ptr(C++11)
smart pointer with unique object ownership semantics
-
shared_ptr(C++11)
smart pointer with shared object ownership semantics
-
weak_ptr(C++11)
weak reference to an object managed by std::shared_ptr
-
auto_ptr(removed in C++17)
smart pointer with strict object ownership semantics
template<class T> class shared_ptr;
std::shared_ptr
is a smart pointer that retains shared ownership of an object through a pointer.
Several shared_ptr
objects may own the same object. The object is destroyed and its memory deallocated when either of the following happens:
- the last remaining shared_ptr owning the object is destroped
- the last remaining shared_ptr owning the object is assigned another pointer via operator= or reset()
The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.
A shared_ptr can share ownership of an object while storing a pointer to another object. This feature can be used to point to member objects while owning the object they belong to while owning the object they belong to. The stored pointer is the one accessed by get(), the dereference and the comparison operators. The managed pointer is the one passed to the deleter when use count reaches zero.
A shared_ptr may also own no objects, in which case it is called empty(an empty shared_ptr may have a non-null stored pointer if the aliasing constructor was used to create it),
-
std::shared_ptr (cplusplus)
template <class T> class shared_ptr;
Manages the storage of a pointer, providing a limited garbage-collection facility, possibly sharing the management with other objects.
Objects of shared_ptr types have the ability of taking ownership of a pointer and share that ownership: once they take ownership, the group of owners of a pointer become responsible for its deletion when the last one of them releases that ownership.
shared_ptr obejcts release onwership on the object they co-own as soon as they themselves are destroyed, or as soon as their value changes either by an assignment oeration or by an explicit call to shared_ptr::reset
. Once all shared_ptr objects that share ownership over a pointer have released thisi ownership, the managed object is deleted(normally by calling ::delete
, but a different deleter may be specified on construction.)