当前位置:网站首页>std::make_ Shared features
std::make_ Shared features
2022-06-22 03:35:00 【A rainy spring night】
Personal essay (Owed by: Happy rain in spring night http://blog.csdn.net/chunyexiyu)
Reference resources :https://stackoverflow.com/questions/20895648/difference-in-make-shared-and-normal-shared-ptr-in-c
std::make_shared Is an interesting function template , Its use , Is used to construct std::shared_ptr object :
#include <memory>
// Both of the following are used to build share_ptr object
std::shared_ptr<Object> p1 = std::make_shared<Object>("foo");
std::shared_ptr<Object> p2(new Object("foo"));
but std::make_shared Different from std::shared_ptr The direct construction process of .
std::shared_ptr Direct construction , Will involve the new Object(“foo”) object , Then inside the constructor new _Ref_count<_Ux>(_Px) Store count , Involving two heap memory allocations .
template<class _Ty> class shared_ptr : public _Ptr_base<_Ty>
template<class _Ty> class _Ptr_base
element_type * _Ptr{
nullptr};
_Ref_count_base * _Rep{
nullptr};
_Set_ptr_rep_and_enable_shared(_Px, new _Ref_count<_Ux>(_Px));
template<class _Ty> class _Ref_count public _Ref_count_base
_Ty * _Ptr;
class __declspec(novtable) _Ref_count_base
_Atomic_counter_t _Uses;
_Atomic_counter_t _Weaks;
std::make_shared When constructing , hold Object Object and the _Ref_count_base The count is put into a _Ref_count_obj Class , once new _Ref_count_obj Allocate it , Only one heap memory allocation is involved ;
Then put the allocated content pointer plus the offset into std::shared_ptr Of _Ptr, _Rep Complete on pointer std::shared_ptr structure .
// stay _Ref_count_obj At the same time object And count items
class __declspec(novtable) _Ref_count_base
_Atomic_counter_t _Uses;
_Atomic_counter_t _Weaks;
template<class _Ty> class _Ref_count_obj public _Ref_count_base
aligned_union_t<1, _Ty> _Storage;
// FUNCTION TEMPLATE make_shared
template<class _Ty, class... _Types>
_NODISCARD inline shared_ptr<_Ty> make_shared(_Types&&... _Args)
{
// make a shared_ptr
const auto _Rx = new _Ref_count_obj<_Ty>(_STD forward<_Types>(_Args)...);
shared_ptr<_Ty> _Ret;
_Ret._Set_ptr_rep_and_enable_shared(_Rx->_Getptr(), _Rx);
return (_Ret);
}
// std::shared_ptr Class and count interface
void _Set_ptr_rep(element_type * _Other_ptr, _Ref_count_base * _Other_rep)
{
// take new resource
_Ptr = _Other_ptr;
_Rep = _Other_rep;
}
From the perspective of memory allocation on the heap ,std::make_shared Use , Compare direct use std::shared_ptr structure , You can reduce the number of heap memory requests at a time ;
meanwhile , Because the memory is together ,ref_count And object The release of is also together , Cannot be released alone ; This is a benefit — Reduce misoperation , Easy to understand ; It is also a drawback — If you want to go ahead alone Object Transfer to another object , You can't do it .
Personal essay (Owed by: Happy rain in spring night http://blog.csdn.net/chunyexiyu)
边栏推荐
猜你喜欢

3de 机器人吸盘抓box

How to synchronize the oak camera?

Selenium entry level project - Doudou quiz

Rabbmitmq simple mode < 1 >

Vs loading symbols causes program to start slowly

3DE recover from design

多线程 interrupt用法

【Leetcode】17回溯(电话号码的字母组合)

【NVMe2.0b 5】NVM Subsystem

(问题解决) 缺少gcr.io/kubebuilder/kube-rbac-proxy:v0.8.0
随机推荐
Template as interface
Why is the first program a programmer writes "Hello world!"
C语言整数取值范围-负数多1的问题
Implementation of synchronization and atomic operation by mutex mutex in golang concurrent programming
多线程 interrupt用法
3de 移动物体的位置
Sword finger offer 68 - ii Nearest common ancestor of binary tree
618来袭,当贝X3和极米H3S怎么选?带你全方位深度解析
[qnx hypervisor 2.2 user manual]5.5 starting and using guest
Summary of image classification based on pytoch: swing transformer
c# 自定义排序
Factory mode
倍福TwinCAT3第三方伺服电机——以汇川IS620N伺服为例子
mysql 查询表的字段的属性、注释、字段信息
float浮点数理解
倍福TwinCAT3伺服控制常用功能块的实现
76. minimum covering substring sliding window method
3de 保存到收藏夹
利用yolov5训练自己的数据集; yolov5的安装与使用 ; yolov5源码解读
mysql-索引创建、优化分析、索引优化