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 ...
随机推荐
- ASP.NET中maxRequestLength和maxAllowedContentLength的区别;上传大文件设置IIS7文件上传的最大大小
https://blog.csdn.net/qq_23663693/article/details/89920039 maxRequestLength表示ASP支持的最大请求大小,而maxAllowe ...
- return chain.filter(exchange); 这句啥意思
答:继续往后执行过滤器,如果不调用这句代码,请求就不会发给控制器了,如果当前执行的过滤器后面还有过滤器,执行那个过滤器,如果没有,就执行控制器. 那我此时想一个请求取消token校验,得在这里加吗? ...
- while循环内使用for循环
一个基础问题,while循环内使用for循环,且当for循环内有控制while循环条件时. 如下所示: bool flag = true; while(flag) { for(i=0;i<10; ...
- mysql 导入问题排查
ERR] 2006 - MySQL server has gone away -- 查询最大数 show global variables like 'max_allowed_packet'; -- ...
- excel、word、PPT中插入PDF文件不显示图标问题
插入PDF对象,不显示正确的PDF图标 临时解决办法:手动修改对象图标 具体位置,可在C:\Windows\Installer目录下搜索PDFFile_8.ico 尝试如下操作: 手动复制生成C:\W ...
- equals的用法的注意事项
String a="equals的用法"; String b=a.equals("equals的用法")?"相等":"不相等&qu ...
- maven加载本地的jar包
方式1 ,通过scope = system的方式加载 <dependency> <groupId>com.sun.jna</groupId> <artifac ...
- 对VC中有关数据类型转换的整理
原文地址:http://spaces.msn.com/wsycqyz/blog/cns!F27CB74CE9ADA6E7!152.trak 对VC中有关数据类型转换的整理 说明:本文纯粹是总结一下 ...
- 常用软件版本记录 lisoaring
操作系统 windows XP(2014年4月8日) windows 7(2020年1月14日终止支持) wiindows 10 Internet Explorer 1995-20220507 Fla ...
- Unity中的深度测试相关知识与问题
https://www.jianshu.com/p/f420b55edd0b?utm_campaign=hugo