Namespaces
Variants
Views
Actions

std::shared_ptr

From cppreference.com
< cpp‎ | memory
Revision as of 22:41, 24 December 2012 by Cubbi (Talk | contribs)

 
 
 
 
 
Defined in header <memory>
template< class T > class shared_ptr;
(since C++11)

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 when the last remaining shared_ptr pointing to it is destroyed or reset. The object is destroyed using delete-expression or a custom deleter that is supplied to shared_ptr during construction.

A shared_ptr may also own no objects, in which case it is called empty.

shared_ptr meets the requirements of CopyConstructible and CopyAssignable.

Contents

Member types

Member type Definition
element_type T

Member functions

Template:cpp/memory/shared ptr/dcl list constructorTemplate:cpp/memory/shared ptr/dcl list destructorTemplate:cpp/memory/shared ptr/dcl list operator=Template:cpp/memory/shared ptr/dcl list resetTemplate:cpp/memory/shared ptr/dcl list swapTemplate:cpp/memory/shared ptr/dcl list getTemplate:cpp/memory/shared ptr/dcl list operator*Template:cpp/memory/shared ptr/dcl list use countTemplate:cpp/memory/shared ptr/dcl list uniqueTemplate:cpp/memory/shared ptr/dcl list operator boolTemplate:cpp/memory/shared ptr/dcl list owner before
Modifiers
Observers

Non-member functions

Template:cpp/memory/shared ptr/dcl list make sharedTemplate:cpp/memory/shared ptr/dcl list allocate sharedTemplate:cpp/memory/shared ptr/dcl list pointer castTemplate:cpp/memory/shared ptr/dcl list get deleterTemplate:cpp/memory/shared ptr/dcl list operator cmpTemplate:cpp/memory/shared ptr/dcl list operator ltltTemplate:cpp/memory/shared ptr/dcl list swap2
Template:cpp/memory/shared ptr/dcl list atomic

Helper classes

Template:cpp/memory/shared ptr/dcl list hash

Implementation notes

In a typical implementation, std::shared_ptr holds only two pointers:

  • a pointer to the managed object
  • a pointer to control block

The control block is a dynamically-allocated object that holds:

  • either a pointer to the managed object or the managed object itself
  • the deleter (type-erased)
  • the allocator (type-erased)
  • the number of shared_ptrs that own the managed object
  • the number of weak_ptrs that refer to the managed object

When shared_ptr is created by calling std::make_shared or std::allocate_shared, the memory for both the control block and the managed object is created with a single allocation. The managed object is constructed in-place in a data member of the control block. When shared_ptr is created via one of the shared_ptr constructors, the managed object and the control block must be allocated separately. In this case, the control block stores a pointer to the managed object.

The pointer held by the shared_ptr directly is the one returned by Template:rlpf, while the pointer/object held by the control block is the one that will be deleted when the number of shared owners reaches zero. These pointers are not necessarily equal.

The destructor of shared_ptr decrements the number of shared owners of the control block. If that counter reaches zero, the control block calls the destructor of the managed object. The control block does not deallocate itself until the std::weak_ptr counter reaches zero as well.