当前位置:网站首页>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使用
边栏推荐
- 图书管理数据库系统设计
- [深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
- 宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
- [notes] Apocalypse: list of practical experience and reflection of product managers
- 诺基亚扩大与英国电信的5G协议,将成其最大无线接入设备供应商
- Libgdx learning path 01: libgdx introduction and running tutorial
- English article translation - English article translation software - free batch translation
- Application of time series database in Hydropower Station
- VAE: understanding and implementation of variational self encoder
- SaltStack之return与job管理
猜你喜欢

CVPR19 - 调参干货《Bag of Tricks for Image Classification with Convolutional Neural Network》

Gmoea code operation 2 -- establishment and operation of operation environment

Cvpr21 unsupervised anomaly detection cutpaste:self supervised learning for anomaly detection and localization

一家芯片公司倒在了B轮

adb remount of the / superblock failed: Permission denied

Convertible bond concept table x notation gives you a convenient and fast experience!

VAE:变分自编码器的理解与实现

NDK 系列(5):JNI 从入门到实践,爆肝万字详解!

idea properties文件显示\u不显示中文的解决

After several twists and turns, how long can the TSDB C-bit of influxdb last?
随机推荐
SQL audit tool self introduction owls
英文翻译意大利语-批量英文翻译意大利语工具免费
idea properties文件显示\u不显示中文的解决
New this prototype precompiled exercise
JS modify table font and table border style
Application of time series database in monitoring operation and maintenance platform
彻底理解位运算——左移、右移
SaltStack之数据系统
Application of time series database in museum environment detection
调用整数或字符数组函数里的参数应该传入啥
Image processing web application development tutorial
App自动化测试是怎么实现H5测试的
Failed to install app-debug.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
lua语言的左对齐函数(手写)
Asp net MVC web development tutorial
NDK 系列(5):JNI 从入门到实践,爆肝万字详解!
Adobe Flash player 34.0.0.92 and available version modification methods (2021-01-23
App自动化测试是怎么实现H5测试的
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
Cvpr21 unsupervised anomaly detection cutpaste:self supervised learning for anomaly detection and localization