当前位置:网站首页>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
边栏推荐
猜你喜欢
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
边境的悍匪—机器学习实战:第二章 端到端的机器学习项目
Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"
Atmospheric particulate matter PMF source analysis
SSH-RSA密钥
VSCode隐藏左边活动栏
高交会重要活动之一|2020中国硬件创新大赛全国总决赛
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及GPIO初始化参数配置】
Kunlun State Screen Production (serialization 4) --- Basics (graphical setting and display, button lights)
查看 word版本号
随机推荐
边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程
边境的悍匪—机器学习实战:第二章 端到端的机器学习项目
思谋面试准备
C语言,库函数中qsort的用法,及解释
Antd 树拖拽一些细节,官网没有,摸坑篇
xxx is not in the sudoers file.This incident will be reported错误
QT连载4:基于QT和STM32H750的LORA试验平台(3)
Difference between logical shift right and arithmetic right shift
Antd简单启动一个企业级项目
Insertion Sort in Classic Sort
QT每周技巧(1)~~~~~~~~~运行图标
通过位运算进行字符大小写转换
联影医疗一面
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
js 替换字符串中所有 “ 引号 —— 数据处理
华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
主机和从机配置,建立ssh连接实现Rviz远程控制
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及TIM定时中断初始化参数配置】
如何开发出成功的硬件产品,一个产品由概念的产生到产品的落地量产又需要经历哪些流程呢?
QT连载1:readyRead()函数,数据分包不完整解决办法