当前位置:网站首页>shared_ptr 和 make_shared 的使用
shared_ptr 和 make_shared 的使用
2022-07-28 17:40:00 【标biao】
std::shared_ptr
是一种智能指针(本身就是一个对象),指向一个对象。它能够记录多少个 shared_ptr 共同指向一个对象,从而消除显示的调用 delete,当引用计数变为零的时候就会将对象自动删除。
std::shared_ptr 可以通过 get() 方法来获取原始指针,通过 reset() 来减少一个引用计数, 并通过use_count()来查看一个对象的引用计数。
//创建了一个 40字节内存空间 的对象,pointer 指向这个对象
std::shared_ptr pointer = new int(10);
std::shared_ptr pointer2 = pointer; // pointer2 也指向这个对象,且引用计数+1
int *p = pointer.get(); // 这样不会增加引用计数
std::cout << "pointer.use_count() = " << pointer.use_count() << std::endl; // 3
std::cout << "pointer2.use_count() = " << pointer2.use_count() << std::endl; // 3
//创建一个指针ptrB 栈对象,构造函数中没有任何参数,因此默认指向的是null。
std::shared_ptr<int> ptrB();
//这种写法也可以,{}表示赋初值或者说初始化,其它类型变量定义也是如此,是c++11的语法特性
std::shared_ptr<int> ptrB{std::make_shared<int>(5)};注意:std::make_shared不支持数组。
std::make_shared
主要功能是在动态内存中分配一个对象并初始化它,返回指向此对象的shared_ptr。
//p1指向一个值为"9999999999"的string对象
shared_ptr<string> p1 = make_shared<string>(10, '9');
//p2指向一个值为"hello"的string对象
shared_ptr<string> p2 = make_shared<string>("hello");
//p2指向一个为空的string对象
shared_ptr<string> p3 = make_shared<string>();
//这种写法也可以,{}表示赋初值或者说初始化,其它类型变量定义也是如此,是c++11的语法特性
std::shared_ptr<int> ptrB{std::make_shared<int>(5)};
和上面直接使用 new<string>("hello")相比,有什么好处呢:
看下面两张图:内存分配可以一次性完成. 这减少了内存分配的次数, 而内存分配是代价很高的操作.


参考文章:
std::shared_ptr的使用_lyshiba的博客-CSDN博客_shared_ptr使用
边栏推荐
- Update of objects in ES6
- App自动化测试是怎么实现H5测试的
- SQL audit tool self introduction owls
- 在矩池云快速安装torch-sparse、torch-geometric等包
- Pagoda panel construction novel CMS management system source code measurement - thinkphp6.0
- Force buckle 1331. Array serial number conversion
- IMU 加热
- Avoidance Adjusted Climbrate
- 彻底理解位运算——与(&)、非(~)、或(|)、异或(^)
- Dockler的基础用法
猜你喜欢

Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters

企业级分布式爬虫框架入门

Rust Getting Started Guide (modules and engineering structures)

App自动化测试是怎么实现H5测试的

Application of time series database in intelligent power consumption field

adb remount of the / superblock failed: Permission denied

JS modify table font and table border style

After several twists and turns, how long can the TSDB C-bit of influxdb last?

英文翻译阿拉伯语-批量英文翻译阿拉伯语工具免费

idea properties文件显示\u不显示中文的解决
随机推荐
How to use Qianqian listening sound effect plug-in (fierce Classic)
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商
微信公众号授权登录后报redirect_uri参数错误的问题
Design of library management database system
App自动化测试是怎么实现H5测试的
SaltStack入门
图书管理数据库系统设计
R language and data analysis practice 11 - data deletion
Adobe XD web design tutorial
当CNN遇见Transformer《CMT:Convolutional Neural Networks Meet Vision Transformers》
文章翻译软件-批量免费翻译软件支持各大翻译接口
Learn from Li Mu, deep learning - linear regression and basic optimization function
Sword finger offer II 109. unlock the password lock
Application of time series database in monitoring operation and maintenance platform
Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
Quickly install torch spark, torch geometric and other packages in moment pool cloud
Transformer for anomaly detection - instra "painting transformer for anomaly detection"
Swing事件处理的过程是怎样的?
Sudo rosdep init error: cannot download default