当前位置:网站首页>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)

原网站

版权声明
本文为[A rainy spring night]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220315370297.html