理解std::shared_ptr In C++

  • std::shared_ptr

Defined in header , which is part of the dynamic memory management library.

Detail in could be found here.

  • Dynamic memory management

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

Smart pointers enable automatic, exception-safe, object lifetime management. Defined in header .

  • Pointer categories
  1. unique_ptr(C++11)

    smart pointer with unique object ownership semantics

  2. shared_ptr(C++11)

    smart pointer with shared object ownership semantics

  3. weak_ptr(C++11)

    weak reference to an object managed by std::shared_ptr

  4. auto_ptr(removed in C++17)

    smart pointer with strict object ownership semantics

  • std::shared_ptr
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:

  1. the last remaining shared_ptr owning the object is destroped
  2. 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),

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.)

  • References

  1. cppreference.com std::shared_ptr
  2. C++ ownership semantics
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值