当前位置:网站首页>Explore the efficiency of make_shared
Explore the efficiency of make_shared
2022-07-30 07:20:00 【Yin Pinghua】
Why Make_shared?
C++11 introduced smart pointers, and there is also a template function std::make_shared that returns a std::shared_ptr of a specified type, which is the same asstd::shared_ptr What benefits can it bring us over the constructor?
Benefits
More efficient
shared_ptr Need to maintain reference count information,
- A strong reference to keep track of how many live shared_ptrs are currently holding the object. The shared object will be destroyed (and possibly released) when the last strong reference leaves.
- Weak reference, used to record how many weak_ptrs are currently observing the object. When the last weak reference leaves, the shared internal information control block will be destroyed and released (the shared object will also be released, ifIf it has not been released yet).
If you allocate the object by using the original new expression and then pass it to the shared_ptr (that is, using the shared_ptr constructor), the shared_ptr implementation has no choice but to allocate the control block separately:
12 | |

If you choose to use make_shared, the situation will become as follows:
1 | |

The action of memory allocation can be done all at once. This reduces the number of memory allocations, which are expensive operations.
About the performance test of the two methods can be seen here Experimenting with C++ std::make_shared
Exception safe
Look at the code below:
1234 | |
C++ does not guarantee the evaluation order of parameters and the evaluation order of internal expressions, so the possible execution order is as follows:
- new Lhs("foo"))
- new Rhs("bar"))
- std::shared_ptr
- std::shared_ptr
Ok, now let's assume that an exception is thrown in the second step (such as out of memory, in short, the Rhs constructor is abnormal), then the memory of the Lhs object applied for in the first step is leaked. ThisThe core of the problem is that shared_ptr does not get the raw pointer immediately.
We can fix this problem as follows.
123 | |
Of course, the recommended practice is to use std::make_shared instead:
1 | |
Disadvantages
Cannot use make_shared when the constructor is protected or private
make_shared is good, but there are some problems, for example, when the object I want to create does not have a public constructor, make_shared cannot be used, of courseWe can use some little tricks to solve this problem, like here How do I call ::std::make_shared on a class with only protected or private constructors?
The memory of the object may not be reclaimed in time
make_shared allocates memory only once, which seems fine. Reduces memory allocation overhead. Here comes the problem, weak_ptr keeps control blocks (strong references, andWeak reference information) life cycle, and therefore keep the memory allocated by the object, the memory will be released only when the last weak_ptr leaves the scope. When the original strong reference is reduced to 0The memory that can be released has now become a strong reference. If the reference is reduced to 0, it can be released, which unexpectedly delays the time of memory release. This is a problem that needs attention for scenarios with high memory requirements. About this issueSee here make_shared, almost a silver bullet
Reference
边栏推荐
- 【Qingdao Station】High-level application of SWAT model and modeling of areas without data, uncertainty analysis and climate change, improvement of land use surface pollution impact model and case analy
- this是什么(你不知道的JS)
- 暂时存着阿里云
- 你不知道的JS思考题
- QT Weekly Skills (1) ~~~~~~~~~ Running Icon
- 2021-09-19 集成学习TASK2
- 自定义类加载器
- 爬2.12.6的Antd上传图片坑
- js 替换字符串中所有 “ 引号 —— 数据处理
- 2021-09-16 集成学习上--task1机器学习数学基础
猜你喜欢

与所有 ARM 工具、软件兼容?韦斯佰瑞这款MCU内核值得关注!

FPGA解析B码----连载1

TCP为什么要三次握手,握手过程中丢包会怎么样?

This beta version of Typora is expired, please download and install a newer;解决方法

Flood Control Assessment Report Compilation Method and Flood Modelling under the New Guidelines (HEC-RAS)

Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
CPU的三种工作模式:实模式、保护模式、长模式

FPGA解析B码----连载2

Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"

2021-09-16 集成学习上--task1机器学习数学基础
随机推荐
昆仑通态屏幕制作(连载3)---基础篇(按钮串口发送)
influxDB运维记录
闭包(你不知道的JS)
"R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment
FPGA parsing B code----serial 2
如何开发出成功的硬件产品,一个产品由概念的产生到产品的落地量产又需要经历哪些流程呢?
虚拟机栈帧结构
干货:线上下单应知应会,快来了解下
QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”
FPGA解析B码----连载1
二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通
2021 soft exam intermediate pass
Flood Control Assessment Report Compilation Method and Flood Modelling under the New Guidelines (HEC-RAS)
vs compile boost library script
Difference between logical shift right and arithmetic right shift
服务器基础知识:包含基本概念,作用,服务器选择,服务器管理等(学习来自米拓建站)
sizeof和strlen最全区别,以及指针和数组运算解析
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
昆仑通态屏幕制作(连载2)---基础篇(设定与显示,串口发送)
求职准备知识点