当前位置:网站首页>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
边栏推荐
- source insight项目导入和使用教程
- Preliminary learning function (3rd blog)
- 测试开发备忘
- Fantasy 5 (ue5) game engine complete course 2022
- 峰值速率超2Gbps!高通在中国首发通过5G毫米波MIMO OTA测试
- 图书管理数据库系统设计
- Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
- 英语文章翻译-英语文章翻译软件-免费批量翻译
- 德国、葡萄牙均宣布不会禁用华为5G设备,但德国会设定严格限制条件!
- 这种动态规划你见过吗——状态机动态规划之股票问题(下)
猜你喜欢

English article translation - English article translation software - free batch translation

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

这个客制化键盘,秀翻我了~

使用SaltStack自动化部署LNMP

文章翻译软件-批量免费翻译软件支持各大翻译接口

Rust Getting Started Guide (modules and engineering structures)

Saltstack system initialization

SQL audit tool self introduction owls

Quickly install torch spark, torch geometric and other packages in moment pool cloud

CVPR21-无监督异常检测《CutPaste:Self-Supervised Learning for Anomaly Detection and Localization》
随机推荐
Getting started with saltstack
adb remount of the / superblock failed: Permission denied
Convertible bond concept table x notation gives you a convenient and fast experience!
SaltStack系统初始化
The mystery of ID number
Taking the opportunity of digital transformation, how can 3C enterprises achieve efficient collaboration through SRM supplier cloud collaboration platform?
【已解决】AC86U ML改版固件虚拟内存创建失败,提示USB磁盘读写速度不满足要求
BLDC 6步换相 simulink
Rust 入门指南(modules 和工程结构)
JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
SaltStack之return与job管理
在矩池云快速安装torch-sparse、torch-geometric等包
Avoidance Adjusted Climbrate
当CNN遇见Transformer《CMT:Convolutional Neural Networks Meet Vision Transformers》
Pytoch: quickly find the main diagonal elements and non diagonal elements of NxN matrix
Saltstack configuration management
为研发高端光刻胶,晶瑞股份斥资7500万元购买SK海力士的ASML光刻机
BLDC 6-step commutation simulink
Avoidance Adjusted Climbrate
Prometheus部署