智能指针auto_prt的使用(c++学习笔记)

2023-10-27

c++中如果要申请资源一般用到new,最后释放资源delete,如果我们在delete之前就退出了函数呢,看下面的代码:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. //#include <memory>  
  3. using namespace std;  
  4.   
  5. void fun()  
  6. {  
  7.     int *ptr = new int;  
  8.       
  9.     if(ptr == 0)  
  10.     {  
  11.         delete ptr;  
  12.         return;  
  13.     }  
  14.   
  15.     cout << "hello prt" << endl;  
  16.   
  17.     delete ptr;  
  18. }  
  19.   
  20. int main()  
  21. {  
  22.     fun();  
  23.     return 0;  
  24. }  

 

这样我们就要多写一个delete,如果if有很多分支或者别情况呢。甚至有时候我们忘了delete呢,他将导致内存泄露。如果使用智能指针auto_ptr就不会出现这种情况了。
下面我们来看一个例子:

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <memory>//auto_ptr 需要用到的头文件  
  3. using namespace std;  
  4.   
  5. void fun(auto_ptr<int> ptr)  
  6. {  
  7.     if (ptr.get() == NULL)  
  8.     {  
  9.         cout << "NULL" << endl;  
  10.     }  
  11.     else  
  12.     {  
  13.         cout << *ptr << endl;  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     auto_ptr<int> ptr(new int);  
  20.     //auto_ptr<int> ptr = new int(11);  //ERROR  注意智能指针不能使用这种赋值方式  
  21.     *ptr = 44;  
  22.     fun(ptr);  
  23.   
  24.     //cout << *ptr << endl;  
  25.     if (ptr.get() == NULL)  
  26.     {  
  27.         cout << "delete" << endl;  
  28.     }  
  29.       
  30.     return 0;  
  31. }  

编译后结果为 44    delete
很明显我们可以看到ptr被释放掉了。
注意,智能指针有拥有权转移的性质,ptr1给ptr2赋值后,ptr1就被delete掉了就不能再取他的值了。代码如下:

[c-sharp]  view plain  copy
  1. #include <iostream>  
  2. #include <memory>  
  3. using namespace std;  
  4.   
  5. ostream& operator<< (ostream& otm , auto_ptr<int>& ptr)  
  6. {  
  7.     if(ptr.get() == NULL)  
  8.     {  
  9.         otm << "NULL" << endl;  
  10.     }  
  11.     else  
  12.     {  
  13.         otm << *ptr << endl;  
  14.     }  
  15.   
  16.     return otm;  
  17. }  
  18.   
  19. int main()  
  20. {  
  21.     auto_ptr<int> ptr1(new int(22));  
  22.     auto_ptr<int> ptr2;  
  23.   
  24.     cout << ptr1 << endl;  
  25.     cout << ptr2 << endl;  
  26.   
  27.     ptr2 = ptr1;  
  28.     cout << ptr1 << endl;  
  29.     cout << ptr2 << endl;  
  30.       
  31.     return 0;  
  32. }  

编译后结果为22  NULL  NULL  22
可以看到执行ptr2 = ptr1后ptr1指向了NULL。
如果我们不希望看到拥有权的改变那就要在声明是加const。拥有权不可以改变但你可以取他的值来赋值,例如:*ptr1 = * prt2。

 

 

下面附一份《c++标准程序库》中提供的auto_ptr的实现代码,看了下,跟《c++程序设计语言》中的差不多,但跟具体,据说比标准库的有一些改进。

[cpp]  view plain  copy
  1. /* The following code example is taken from the book 
  2.  * "The C++ Standard Library - A Tutorial and Reference" 
  3.  * by Nicolai M. Josuttis, Addison-Wesley, 1999 
  4.  * 
  5.  * (C) Copyright Nicolai M. Josuttis 1999. 
  6.  * Permission to copy, use, modify, sell and distribute this software 
  7.  * is granted provided this copyright notice appears in all copies. 
  8.  * This software is provided "as is" without express or implied 
  9.  * warranty, and with no claim as to its suitability for any purpose. 
  10.  */  
  11. /* class auto_ptr 
  12.  * - improved standard conforming implementation 
  13.  */  
  14. namespace std {  
  15.     // auxiliary type to enable copies and assignments (now global)  
  16.     template<class Y>  
  17.     struct auto_ptr_ref {  
  18.         Y* yp;  
  19.         auto_ptr_ref (Y* rhs)  
  20.          : yp(rhs) {  
  21.         }  
  22.     };  
  23.   
  24.     template<class T>  
  25.     class auto_ptr {  
  26.       private:  
  27.         T* ap;    // refers to the actual owned object (if any)  
  28.       public:  
  29.         typedef T element_type;  
  30.   
  31.         // constructor  
  32.         explicit auto_ptr (T* ptr = 0) throw()  
  33.          : ap(ptr) {  
  34.         }  
  35.   
  36.         // copy constructors (with implicit conversion)  
  37.         // - note: nonconstant parameter  
  38.         auto_ptr (auto_ptr& rhs) throw()  
  39.          : ap(rhs.release()) {  
  40.         }  
  41.         template<class Y>  
  42.         auto_ptr (auto_ptr<Y>& rhs) throw()  
  43.          : ap(rhs.release()) {  
  44.         }  
  45.           
  46.         // assignments (with implicit conversion)  
  47.         // - note: nonconstant parameter  
  48.         auto_ptr& operator= (auto_ptr& rhs) throw() {  
  49.             reset(rhs.release());  
  50.             return *this;  
  51.         }  
  52.         template<class Y>  
  53.         auto_ptr& operator= (auto_ptr<Y>& rhs) throw() {  
  54.             reset(rhs.release());  
  55.             return *this;  
  56.         }  
  57.           
  58.         // destructor  
  59.         ~auto_ptr() throw() {  
  60.             delete ap;  
  61.         }  
  62.   
  63.         // value access  
  64.         T* get() const throw() {  
  65.             return ap;  
  66.         }  
  67.         T& operator*() const throw() {  
  68.             return *ap;  
  69.         }  
  70.         T* operator->() const throw() {  
  71.             return ap;  
  72.         }  
  73.   
  74.         // release ownership  
  75.         T* release() throw() {  
  76.             T* tmp(ap);  
  77.             ap = 0;  
  78.             return tmp;  
  79.         }  
  80.   
  81.         // reset value  
  82.         void reset (T* ptr=0) throw() {  
  83.             if (ap != ptr) {  
  84.                 delete ap;  
  85.                 ap = ptr;  
  86.             }  
  87.         }  
  88.   
  89.         /* special conversions with auxiliary type to enable copies and assignments 
  90.          */  
  91.         auto_ptr(auto_ptr_ref<T> rhs) throw()  
  92.          : ap(rhs.yp) {  
  93.         }  
  94.         auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() {  // new  
  95.              reset(rhs.yp);  
  96.              return *this;  
  97.         }  
  98.         template<class Y>  
  99.         operator auto_ptr_ref<Y>() throw() {  
  100.             return auto_ptr_ref<Y>(release());  
  101.         }  
  102.         template<class Y>  
  103.         operator auto_ptr<Y>() throw() {  
  104.             return auto_ptr<Y>(release());  
  105.         }  
  106.     };  
  107. }  



FROM: http://blog.csdn.net/z0203153008/article/details/4652046






本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

智能指针auto_prt的使用(c++学习笔记) 的相关文章

  • Unity第一人称视角相机跟随鼠标移动

    基于Unity实现第一人称视角相机跟随鼠标移动 private float xRotate 0 0f private float yRotate 0 0f Void Update xRotate Input GetAxis Mouse Y
  • Vue-cli 相关webpack配置-量化打包时间和打包后分析

    参考 https blog csdn net weixin 41779718 article details 110038340 主要介绍两个插件 用于量化打包时间 和打包后分析 speed measure webpack plugin 可
  • Android中Fragmnet的跳转

    1 Fragment跳转到Fragment中 描述 我的项目是由MainActivity启动的Fragment作为底部导航栏 HomeFragment StoreFragment PersonalFragment 情景一 点击HomeFra
  • HTTP 1.1与HTTP 1.0的比较

    HTTP 1 1与HTTP 1 0的比较 一个WEB站点每天可能要接收到上百万的用户请求 为了提高系统的效率 HTTP 1 0规定浏览器与服务器只保持短暂的连接 浏览器的每次请求都需要与服务器建立一个TCP连接 服务器完成请求处理后立即断开
  • Allegro学习笔记:Allegro快速入门之一--查看BRD Layout

    由于工作需要 需要学习在Allegro下查看以及简单修改pcb 然而在网上找了一阵子资料 发现很多资料都很零碎 不成系统 而成系统的 又把读者当成要学习用Allegro Layout的对象 从头到尾完整的讲解 但是虽然完整 却并不详细 导致
  • 代码审计工具学习之RISP(安装以及初步操作)

    目录 1 代码审计 1 1名词解释 1 2代码审计的重要性 1 3代码审计的步骤 1 4代码审计的内容 1 5常见的代码审计工具 2 RIPS 2 1什么是RIPS 2 2 RIPS的安装过程 3典型漏洞分析 3 1 文件包含漏洞分析 3
  • Vue3/ 父组件 ref 获取子组件内属性或方法 子组件 defineExpose 暴露方法 或 属性 总结、

    一 父组件 ref 获取子组件内属性或方法 总结 介绍 Vue3中 父组件拿到 子组件内方法 属性 可以通过 ref 但是将 ref 插入子组件标签身上后 打印出来是 什么都没有的 是空的 这个是为什么呢 哦 原来 Vue3严格性问题 属性
  • 崩溃了没有生成dump的分析步骤

    文章目录 1 系统日志的错误信息 2 分析崩溃 1 获取exe和pdb 2 ide打开对应exe 3 找基址 4 查找错误位置 很大可能 在用户现场的时候 可能存在崩溃了但是没有生成dump的情况 可能只能有一些系统日志或者直接给你个截图什
  • Oracle 11g ADG(Active Data Guard)切换脚本的示例

    以下是一个Oracle 11g ADG Active Data Guard 切换脚本的示例 用于将ADG从当前主服务器切换到备用服务器 bin bash Set Oracle environment variables ORACLE SID
  • Ubuntu22.04 安装Anaconda

    文章目录 1 下载Anaconda 2 安装Anaconda 3 启动环境 4 常用命令 4 1创建虚拟环境 4 2查询当前系统conda虚拟环境 4 3挂载虚拟环境 4 4退出虚拟环境 4 5查看已安装的工具包 4 6删除虚拟环境 1 下
  • python文件的写入

    wirte 方法把字符串写入文件 writelines 方法可以把列表中存储的内容写入文件 f file hello txt w li hello world n hello china n f writelines li f close
  • ROS获取串口信息及后续处理(以惯导IMU XW-GI5651为例)

    一 问题简介 自动驾驶小车的底层的数据相当一部分是通过串口发送的 以惯导为例 惯导的定位信息大概如下所示 GPFPD 0 1666 330 0 000 0 015 0 129 0 00000000 0 00000000 0 000 0 00
  • Pycharm激活方法

    pycharm 激活 JetBrains IDEA 系列产品通用激活方法 1 打开激活窗口 2 选择 Activate new license with License server 用license server 激活 3 在 Licen

随机推荐

  • python子类继承父类参数传递的简便写法

    当父类参数较多 子类又要增加新的参数时 如果一个一个照抄一遍参数容易出错 这里可以借助 args来简便的进行父类初始化 利用 父类My sprit有一大堆参数 子类UAV需要增加三个新参数scout range communicate ra
  • Java多线程安全问题和锁

    多线程安全问题和锁 文章目录 多线程安全问题和锁 线程在jvm中的特点 锁的出现 synchronized 关键字 监视器锁monitor lock 死锁的产生和避免 什么是线程安全问题 当多个线程同时操作同一个数据时 可能会出现数据不一样
  • 第14天-ElasticSearch环境配置,构建检索服务及商品上架到ES库

    1 ElasticSearch概念 官网介绍 https www elastic co cn what is elasticsearch 官网学习文档 https www elastic co guide en elasticsearch
  • 三、Vite 文件系统 import.meta.glob

    文章目录 一 参考 二 import meta glob 介绍 2 1 import meta glob 的作用 2 2 eager true 一次引入所有 2 3 Glob 导入注意事项 三 遍历 import meta glob 文件系
  • 水库大坝安全管理主要问题和维护措施

    水库大坝作为防灾减灾 解决水资源流失和供需矛盾的重要利民工程 对于维护社会稳定 确保社会经济持续发展具有非常重要的作用 但由于我国大部分水库都建设于 20世纪 70 80 年代 如山东省棘洪滩水库其建设开工时间为1986 年 当时我国经济条
  • Spark数据类型SparseMatrix

    SparseMatrix Spark的mllib包中提供了机器学习的两种基本数据类型 DenseMatrix 稠密 和 SparseMatrix 稀疏 在初始化对象的时候可以使用Matrices伴生对象产生 先看下示例代码 import o
  • Linux-------------DNS域名解析:正反解析

    DNS域名解析 DNS简介 前期准备 正方解析实验 正向解析实验 反向解析实验 常见错误 修改resolv conf 修改hostname 检查DNS是否为ip地址 归纳总结 DNS简介 域名管理系统DNS Domain Name Syst
  • Pentaho Report Designer 入门教程(二)

    Pentaho Report Designer 入门教程 二 采用Pentaho Report Designer5 1版本 也是最新的版本 一 安装和介绍 介绍部分内容略 首先安装jdk 并配置java相关环境变量 下载pentaho re
  • stm32学习总结:2、搭建基于CLion的stm32开发环境

    stm32学习总结 2 搭建基于CLion的stm32开发环境 文章目录 stm32学习总结 2 搭建基于CLion的stm32开发环境 1 前言 2 相关工具和环境准备 2 1 STM32CubeMX 前置工程创建工具 2 2 CLion
  • 敏捷:什么是用户故事(User Story)

    摘要 一件用户通过系统完成他一个有价值的目标 买一罐饮料 的事 这样的过程就叫 用户案例 user case 或者 用户故事 user story 本文描述了敏捷开发的技巧 如何以用户故事管理项目 什么是用户故事 user story 假定
  • 内网渗透-frp 用于内网穿透的基本配置和使用

    frp 用于内网穿透的基本配置和使用 文章目录 frp 用于内网穿透的基本配置和使用 前言 frps frpc 后记 参考 前言 frp 是一个专注于内网穿透的高性能的反向代理应用 支持 TCP UDP HTTP HTTPS 等多种协议 可
  • 【开源介绍】命令行的艺术( the-art-of-command-line )

    1 概述 转载 命令行的艺术 the art of command line
  • Blob数据类型、数据库事务

    1 Bolb是二进制长对象的意思 通常用于存储大文件 通过二进制数据保存到数据库里 并可以从数据库里恢复指定文件 2 如果需要将图片插入数据库 不能通过普通的SQL语句完成 Bolb常量无法表示 所以将Bolb数据插入数据库需要使用Prep
  • 合成孔径SAR卫星影像专业术语

    转载自 http www kosmos image com index php m content c index a show catid 73 id 4132 目录 Across track Active Remote Sensing
  • 初学机器学习:直观解读KL散度的数学概念

    选自thushv com 作者 Thushan Ganegedara 机器之心编译 机器学习是当前最重要的技术发展方向之一 近日 悉尼大学博士生 Thushan Ganegedara 开始撰写一个系列博客文章 旨在为机器学习初学者介绍一些基
  • 深度学习(Deep Learning)读书思考六:循环神经网络一(RNN)

    概述 循环神经网络 RNN Recurrent Neural Network 是神经网络家族中的一员 擅长于解决序列化相关问题 包括不限于序列化标注问题 NER POS 语音识别等 RNN内容比较多 分成三个小节进行介绍 内容包括RNN基础
  • QT窗体间传值总结之Signal&Slot

    在写程序时 难免会碰到多窗体之间进行传值的问题 依照自己的理解 我把多窗体传值的可以使用的方法归纳如下 1 使用QT中的Signal Slot机制进行传值 2 使用全局变量 3 使用public形式的函数接口 4 使用QT中的Event机制
  • 独立进程使用django模型(django.setup()使用)

    文章目录 独立进程使用django模型 独立进程使用django模型 步骤 django setup RuntimeError populate isn t reentrant 独立进程使用django模型报错 Apps aren t lo
  • 提升Postern代理性能的五个小技巧

    在使用Postern代理时 如何提高其性能是许多用户关注的问题 本文将分享一些针对Postern代理进行优化的技巧和建议 帮助更好地利用该工具并获得更出色的网络体验 Postern是一个功能强大且灵活易用的Android应用程序 可实现全局
  • 智能指针auto_prt的使用(c++学习笔记)

    c 中如果要申请资源一般用到new 最后释放资源delete 如果我们在delete之前就退出了函数呢 看下面的代码 cpp view plain copy include