在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的使用的更多相关文章

  1. 智能指针std::unique_ptr

    std::unique_ptr 1.特性 1) 任意时刻只能由一个unique_ptr指向某个对象,指针销毁时,指向的对象也会被删除(通过内置删除器,通过调用析构函数实现删除对象) 2)禁止拷贝和赋值 ...

  2. C++11智能指针之std::unique_ptr

    C++11智能指针之std::unique_ptr   uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...

  3. (译+原)std::shared_ptr及std::unique_ptr使用数组

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://stackoverflow.com/questions ...

  4. 字符串连接比较(std::unique_ptr实现)

    比较代码之间可能相差大,可是速度相差很大,而且目的在于测试unique_ptr使用...; C/C++: #include <iostream> std::unique_ptr<ch ...

  5. std::unique_ptr的用法

    std::ofstream("demo.txt") << 'x'; // 准备要读的文件 { std::unique_ptr<std::FILE, decltyp ...

  6. 智能指针(1)-std::unique_ptr

    std::unique_ptr std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权.由C++11标准引入,用于替代C++98中过时的std::auto_ ...

  7. std::unique_ptr使用incomplete type的报错分析和解决

    Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...

  8. 智能指针思想实践(std::unique_ptr, std::shared_ptr)

    1 smart pointer 思想 ​ 个人认为smart pointer实际上就是一个对原始指针类型的一个封装类,并对外提供了-> 和 * 两种操作,使得其能够表现出原始指针的操作行为. ​ ...

  9. 关于std:auto_ptr std:shared_ptr std:unique_ptr

    很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生成健壮的代码.本文阐述了如何正确 ...

  10. C++11 unique_ptr智能指针详解

    在<C++11 shared_ptr智能指针>的基础上,本节继续讲解 C++11 标准提供的另一种智能指针,即 unique_ptr 智能指针. 作为智能指针的一种,unique_ptr ...

随机推荐

  1. myForEach

    Array.prototype.myForEach = function (callback, thisArg = undefined) { if (typeof callback !== 'func ...

  2. 【Hive】元数据库部署的三种方式和选择【metaStore server】

    一.Derby 元数据使用之前,要在hive目录下执行schematool命令,进行初始化设置 bin/schematool -dbType derby -initSchema 启动hive后,可以用 ...

  3. Springboot打包部署的步骤

    1.配置pom.xml 引入插件 <groupId>com.bostech</groupId> <artifactId>dcs</artifactId> ...

  4. 浅析Winform的可视样式

    每一个C#的Winform项目的Main方法里,都有这么一行代码,那么它究竟是用来做什么的呢? Application.EnableVisualStyles(); 从注释来看,这是一行用作设置样式的代 ...

  5. easyExcel-modle

    package com.alibaba.easyexcel.test.model;import com.alibaba.excel.annotation.ExcelProperty;import co ...

  6. 解决adb devices无法连接各种模拟器

    经常使用到模拟器的童鞋,如果在使用adb devices命令时,发现出现"List of devices attached",模拟器USB调试都开启的情况下,也没有连接成功.这种情 ...

  7. vue3导出功能

    proxy.$axios.own.get(url,{ responseType: 'blob' //首先设置responseType字段格式为 blob}).then(res => { let ...

  8. 1 wine-stable + 2 brew install mono

    一. 通过wine官网找到安装方法 1  brew tap homebrew/cask-versions2  brew install --cask --no-quarantine (selected ...

  9. xd p3 搭建安全扩展

    常见搭建平台脚本启用 常见平台java Python php jsp搭建要启用脚本 中间件(搭建平台):Apache IIS Tomcat Nginx 主机头值 即 域名 域名IP目录解析安全问题 域 ...

  10. ssl 导入和配置

    https://blog.csdn.net/qq_23663693/article/details/121698553