当前位置:网站首页>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)
边栏推荐
猜你喜欢

Scheduling function: Splunk operator Controller Manager

2019年全国职业院校技能大赛中职组“网络空间安全”正式赛卷及其“答案”

MySQL index creation, optimization analysis and index optimization

ASUS reinstallation system keyboard lamp failure = & gt; Refitting the ATK drive

倍福TwinCAT设置PLC的扫描周期,运行周期方法

Understand virtual memory from the root

A component required a bean of type 'com.example.demo3.service.UserServiceImp' that could not be fou

【NVMe2.0b 5】NVM Subsystem

3de从设计中恢复

618来袭,当贝X3和极米H3S怎么选?带你全方位深度解析
随机推荐
golang并发编程之原子操作详解
服装实体店如何突破销售困境
魔法方法《六》__enter__和__exit__
Rabbmitmq publish subscribe mode < 2 >
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
Usage of tail
Vs loading symbols causes program to start slowly
EU5, eu7, EX3, Ex5 install third-party apps
华硕重装系统键盘灯失效 =&gt;重装ATK驱动
R数据分析:临床预测模型中校准曲线和DCA曲线的意义与做法
Template as interface
我们如何解决了RealSense偏色问题?
Analyzing iceberg merge tasks to resolve data conflicts
Golang standard library time
作为接口的模板
VIM from dislike to dependence (18) -- advanced search mode
C51的一些日记
1299. 将每个元素替换为右侧最大元素
powerdesigner CDM中联系理解
golang并发编程之runtime包