std::unique_ptr release的使用
在c++中,动态内存管理是通过new/delete 运算符来进行的。由于确保在正确的时间释放内存是很困难的,为了避免内存泄漏,更加容易,安全地使用动态内存,C++11标准库提供了两种智能指针类型来管理动态对象。只能指针的行为类似于常规指针,重要的区别是它负责自动释放所指的对象。
std::shared_ptr , 允许多个指针指向同一个对象
std::unique_ptr, 独占所指向的对象
std::unique_ptr 是 c++11中用来取代 std::auto_ptr 指针的指针容器。 它不能与其他unique_ptr类型的指针对象共享所指对象的内存。这种所有权仅能够通过std::move函数来转移。unique_ptr是一个删除了拷贝构造函数、保留了移动构造函数的指针封装类型。
调用release 会切断unique_ptr 和它原来管理的对象的联系。release 返回的指针通常被用来初始化另一个智能指针或给另一个智能指针赋值。如果不用另一个智能指针来保存release返回的指针,程序就要负责资源的释放。
#include <iostream>
#include <memory> int main() { std::unique_ptr<int> uptr(new int(10)); //绑定动态对象
//std::unique_ptr<int> uptr2 = uptr; //不能賦值
//std::unique_ptr<int> uptr2(uptr); //不能拷内
std::unique_ptr<int> uptr2 = std::move(uptr); //轉換所有權 if(uptr == nullptr)
printf("uptr give up *int\n"); int * p = uptr2.release(); //uptr2释放对指针的控制权,返回指针,并将uptr2置为空 if(uptr2 == nullptr)
printf("uptr2 give up *int\n"); printf("%d\n", *p);
delete p; return 0;
}
输出结果:
[daq@centos build]$ ./hello-exe/cmake-good
uptr give up *int
uptr2 give up *int
10
std::unique_ptr release的使用的更多相关文章
- 智能指针std::unique_ptr
std::unique_ptr 1.特性 1) 任意时刻只能由一个unique_ptr指向某个对象,指针销毁时,指向的对象也会被删除(通过内置删除器,通过调用析构函数实现删除对象) 2)禁止拷贝和赋值 ...
- C++11智能指针之std::unique_ptr
C++11智能指针之std::unique_ptr uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...
- (译+原)std::shared_ptr及std::unique_ptr使用数组
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://stackoverflow.com/questions ...
- 字符串连接比较(std::unique_ptr实现)
比较代码之间可能相差大,可是速度相差很大,而且目的在于测试unique_ptr使用...; C/C++: #include <iostream> std::unique_ptr<ch ...
- std::unique_ptr的用法
std::ofstream("demo.txt") << 'x'; // 准备要读的文件 { std::unique_ptr<std::FILE, decltyp ...
- 智能指针(1)-std::unique_ptr
std::unique_ptr std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权.由C++11标准引入,用于替代C++98中过时的std::auto_ ...
- std::unique_ptr使用incomplete type的报错分析和解决
Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...
- 智能指针思想实践(std::unique_ptr, std::shared_ptr)
1 smart pointer 思想 个人认为smart pointer实际上就是一个对原始指针类型的一个封装类,并对外提供了-> 和 * 两种操作,使得其能够表现出原始指针的操作行为. ...
- 关于std:auto_ptr std:shared_ptr std:unique_ptr
很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生成健壮的代码.本文阐述了如何正确 ...
- C++11 unique_ptr智能指针详解
在<C++11 shared_ptr智能指针>的基础上,本节继续讲解 C++11 标准提供的另一种智能指针,即 unique_ptr 智能指针. 作为智能指针的一种,unique_ptr ...
随机推荐
- windows10本地联调zk环境报异常SASL config status: Will not attempt to authenticate using SASL (unknown error)
感谢原文:https://blog.csdn.net/qq_43639296/article/details/123282280 SASL config status: Will not attemp ...
- ChatGPT 爆火!真有那么神?设计师会失业吗?
人工智能来了,咱们是不是都要失业了呢? 一款AI产品,在科技市场和资本市场掀起了一阵风暴. 一切的源头,来自一个由美国人工智能公司OpenAI开发的一种大型语言模型ChatGPT.它采用了Transf ...
- 【博客】如何在Github上创建博客
[博客]如何在Github上创建博客 1. 安装nodejs windows安装npm教程--nodejs 2. 安装hexo npm install -g hexo-cli 3. 搭建博客 $ he ...
- 解决adb devices无法连接各种模拟器
经常使用到模拟器的童鞋,如果在使用adb devices命令时,发现出现"List of devices attached",模拟器USB调试都开启的情况下,也没有连接成功.这种情 ...
- excel数字转日期
import datetime delta = datetime.timedelta() today = datetime.datetime.strptime('1899/12/30', '%Y/%m ...
- vscode 中前端代码不能通过ctrl+鼠标左键点击跳转(亲测可行)
1.ctrl+p 查找 jsconfig.json文件.如果没有,就在根目录下新建jsconfig.json 2.若已经有文件,如下图新增paths 若没有,复制下面内容到jsconfig.json ...
- C++ primer 5th 第一章 开始 阅读笔记
第一章 开始 第一节 编写一个简单的C++程序 不同编译器使用不同的后缀命名约定,比如cc.cpp.c. 比如main程序保存到prog1.cc中,可以使用如下命令来编译它:cc prog1.cc.其 ...
- Review1(C#语言基础)
MeshFilter决定了物体时什么形状 MeshRender决定了物体时的外观: 运行时常量:readonly 1.readonly string NAME_READONLY = "rea ...
- VSCode-WSL配置 C++ —— 以OpenCV4为例
生成并编辑c_cpp_properties.json 命令窗口输入:>C/C++: Edit Configurations(JSON),就会自动生成该文件 在includePath中加上需要in ...
- nop4.3 admin中添加新菜单
感觉跟之前版本区别不是很大,先记录下. 1. 首先在sitemap.config 文件里添加菜单. <siteMapNode SystemName="SystemManage" ...