当前位置:网站首页>shared_ PTR and make_ Use of shared
shared_ PTR and make_ Use of shared
2022-07-28 19:34:00 【Biao】
std::shared_ptr
It's a kind of Intelligent pointer ( Itself is an object ), Point to an object . It can record how many shared_ptr Point to an object together , This eliminates the displayed call delete, When the reference count becomes zero, the object is automatically deleted .
std::shared_ptr Can pass get() Method to get the original pointer , adopt reset() To reduce a reference count , And pass use_count() To see the reference count of an object .
// Created a 40 Byte memory space The object of ,pointer Point to this object
std::shared_ptr pointer = new int(10);
std::shared_ptr pointer2 = pointer; // pointer2 And point to this object , And reference count +1
int *p = pointer.get(); // This does not increase the reference count
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 3
// Create a pointer ptrB Stack object , There are no arguments in the constructor , So the default point is null.
std::shared_ptr<int> ptrB();
// This kind of writing can also ,{} It means to assign initial value or initialize , The same is true for other types of variable definitions , yes c++11 Syntax features of
std::shared_ptr<int> ptrB{std::make_shared<int>(5)}; Be careful :std::make_shared Arrays are not supported .
std::make_shared
The main function is to allocate an object in dynamic memory and initialize it , Returns... Pointing to this object shared_ptr.
//p1 Point to a value of "9999999999" Of string object
shared_ptr<string> p1 = make_shared<string>(10, '9');
//p2 Point to a value of "hello" Of string object
shared_ptr<string> p2 = make_shared<string>("hello");
//p2 Point to an empty string object
shared_ptr<string> p3 = make_shared<string>();
// This kind of writing can also ,{} It means to assign initial value or initialize , The same is true for other types of variable definitions , yes c++11 Syntax features of
std::shared_ptr<int> ptrB{std::make_shared<int>(5)};
Use directly with the above new<string>("hello") comparison , What are the benefits :
Look at the next two pictures : Memory allocation can be completed at one time . This reduces the number of memory allocations , Memory allocation is a costly operation .


Reference article :
std::shared_ptr Use _lyshiba The blog of -CSDN Blog _shared_ptr Use
std::make_shared_zhgeliang The blog of -CSDN Blog _std::make_shared
边栏推荐
- Sword finger offer II 109. unlock the password lock
- JDBC simple encapsulation
- 智能合约安全——溢出漏洞
- andorid系统layout、values、drawable适配
- 串口接收应用——环形缓冲buffer
- 力扣 1331. 数组序号转换
- Rust 入门指南(crate 管理)
- sudo rosdep init 出现 ERROR: cannot download default
- JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
- Share several coding code receiving verification code platforms, which will be updated in February 2022
猜你喜欢

Photoshop responsive web design tutorial

shared_ptr 和 make_shared 的使用

Swing事件处理的过程是怎样的?

WPF implements MessageBox message prompt box with mask

Web 3.0 development learning path

调用整数或字符数组函数里的参数应该传入啥

Fantasy 5 (ue5) game engine complete course 2022

ES6 new - arrow function

Image processing web application development tutorial

Kotlin Android development novice tutorial
随机推荐
开盘暴涨215%!国产信号链芯片企业芯海科技登陆科创板
亚马逊推出Amazon One手掌支付系统,非接触式掌静脉识别市场有望爆发
Application of TSDB in civil aircraft industry
彻底理解位运算——左移、右移
Srs4.0 installation steps
Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
Photoshop web design practical tutorial
sudo rosdep init 出现 ERROR: cannot download default
这个客制化键盘,秀翻我了~
stc12c5a60s2功能说明(STC12C5A60S2默认触发)
C string to short[] method
Transformer for anomaly detection - instra "painting transformer for anomaly detection"
Taking the opportunity of digital transformation, how can 3C enterprises achieve efficient collaboration through SRM supplier cloud collaboration platform?
助力面板行业创新,FPGA将成屏厂TCON最佳选择?
Jestson nano Object detection
我的第二次博客——C语言
VAE: understanding and implementation of variational self encoder
英文翻译葡萄牙语-批量英文转换葡萄牙语-各种语言免费互译转换
【笔记】《启示录》:产品经理的实践经验与反省清单