当前位置:网站首页>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
边栏推荐
- About map custom sorting of keys
- >>> /deep/ ::v-deep 深度作用选择器
- Insertion Sort in Classic Sort
- BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
- TCP为什么要三次握手,握手过程中丢包会怎么样?
- 重磅揭晓!第十四届深创赛福田预选赛区暨华秋第八届硬创大赛华南分赛区晋
- Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
- 工程师必看:常见的PCB检测方法有哪些?
- 虚拟机栈帧结构
- Antd简单启动一个企业级项目
猜你喜欢

QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”

QT serial and CAN dynamic real-time display the log data
![[Quick MSP430f149] Notes on learning MSP430f149 during the game](/img/06/741c609b24be007718091b8348666c.png)
[Quick MSP430f149] Notes on learning MSP430f149 during the game

Acwing刷题第一节

【江科大自化协stm32F103c8t6】笔记之【入门32单片机及GPIO初始化参数配置】

Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)

查看 word版本号

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

Cannnot download sources不能下载源码百分百超详细解决方案

华秋电子成为开放原子开源基金会openDACS捐赠人,共建 openDACS开源生态
随机推荐
【正点原子】sys.c、sys.h位带操作的简单应用
QT serial and CAN dynamic real-time display the log data
Kunlun state screen production (serial 3) - based article (button serial port to send)
华秋第八届硬创大赛携手NVIDIA初创加速计划,赋能企业发展
查看 word版本号
Cannnot download sources不能下载源码百分百超详细解决方案
Sklearn : train_test_split()函数的用法
[Quick MSP430f149] Notes on learning MSP430f149 during the game
VsCode与Sublime编辑器优缺点对比
QT连载4:基于QT和STM32H750的LORA试验平台(3)
OpenLayers (ol包),Vite显示地图(附源码)
爬2.12.6的Antd上传图片坑
一文盘点五款 BLDC 风机参考方案,建议先马
给Vscode配置ESlint语法检查 — ESLint 插件自动格式化设置(实现Ctrl+S 按照ESLint规则自动格式化代码)
关于报错vscode
边境的悍匪—机器学习实战:第一章 机器学习的基础知识
通过位运算进行字符大小写转换
HSPF model application
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
2021 soft exam intermediate pass