智能指针实战讲解--龙之介《Effective C++》实验室
条款13 以对象管理资源
在C++中 我们申请资源(new) 之后需要手动释放资源(delete)
Test* t = new Test();//申请资源......delete t;//释放资源
但是我们不知道
…
…
…
在申请和释放中间是否会发生什么匪夷所思的事情使得程序提前终止未能执行delete。[异常,goto,return]
如果申请的资源未被释放就会造成内存泄漏
Test* t = GetText("t");return;delete t;
为了解决这个问题,神奇的智能指针出现了!!!!
auto_ptr
你只需要把类丢进智能指针他就会自动帮你释放资源。
auto_ptr<Test> t2(GetText("t2"));//不需要delete他会自动delete
其实自动delete的机制就是对象是由栈存储的所以会自动释放内存的机制
智能指针实质是一个对象,行为表现的却像一个指针。
假设有个A类,你创建这个对象,生命周期结束他会自动释放掉
A a;
一次性说太大我怕消化不了。先看看代码
#include <iostream>
#include<string>
using namespace std;
class Test
{public:Test(string test) :test(test){cout << " Test :" <<this->test<< endl;}~ Test() {cout << " ~Test :" <<this->test<< endl;}private:string test;
};Test* GetText(string str){return new Test(str);}class A{public:A();~ A();private:};A:: A(){cout << "a"<<endl;}A::~ A(){cout << "~a" << endl;}void test1(){cout << "test1:" << endl;Test* t = GetText("t");delete t;}void test2(){cout << "test2:" << endl;Test* t = GetText("t");return;delete t;}void text3(){cout << "test3:" << endl;auto_ptr<Test> t2(GetText("t2"));}void text4(){cout << "test4:" << endl;A a;}
int main()
{test1();test2();text3();text4();return 0;
}
运行结果如下:
但是auto_ptr有个缺点,就是他的=operator 会把之前存的引用给删除,意味着,它无法发挥正常的复制行为。
auto_ptr<Test> t1(GetText("t1"));auto_ptr<Test> t2(t1);cout << t1.get()<<endl;cout << t2.get()<<endl;t1 = t2;cout << t1.get() << endl;cout << t2.get() << endl;
解决方案就是 shared_ptr,它是引用计数形智能指针
他会持续追踪共用多少个对象指向某笔资源
cout <<endl<< "test2:" << endl;shared_ptr<Test> t1(GetText("t1"));shared_ptr<Test> t2(t1);cout << t1.get() << endl;cout << t2.get() << endl;t1 = t2;cout << t1.get() << endl;cout << t2.get() << endl;
#include <iostream>
#include<string>
using namespace std;
class Test
{public:Test(string test) :test(test){cout << " Test :" <<this->test<< endl;}~ Test() {cout << " ~Test :" <<this->test<< endl;}private:string test;
};Test* GetText(string str){return new Test(str);}class A{public:A();~ A();private:};A:: A(){cout << "a"<<endl;}A::~ A(){cout << "~a" << endl;}void test1(){cout << endl << "test1:" << endl;auto_ptr<Test> t1(GetText("t1"));auto_ptr<Test> t2(t1);cout << t1.get()<<endl;cout << t2.get()<<endl;t1 = t2;cout << t1.get() << endl;cout << t2.get() << endl;}void test2(){cout <<endl<< "test2:" << endl;shared_ptr<Test> t1(GetText("t1"));shared_ptr<Test> t2(t1);cout << t1.get() << endl;cout << t2.get() << endl;t1 = t2;cout << t1.get() << endl;cout << t2.get() << endl;}
int main()
{test1();test2();return 0;
}
运行结果:
智能指针实战讲解--龙之介《Effective C++》实验室相关推荐
- c++智能指针 示例讲解
智能指针实际上就是通过模板技术实现的一个类 内存泄露(臭名昭著的Bug)--在软件开发和测试阶段都不容易被发现 -动态申请堆空间,用完后不归还 -C++语言中没有垃圾回收的机制 -指针无法控制所指堆空 ...
- C++---智能指针原理讲解
为什么需要智能指针 在写代码的时候,从堆上申请的空间,由于一些原因(代码中途异常),没有得到释放,从而导致代码出现内存泄漏,所以为了防止此类问题的出现,从而出现智能指针.采用智能指针可以自动检测,指针 ...
- 【Smart_Point】动态内存与智能指针实战:文本查询程序(设计set,map,智能指针的应用)
文章目录 Cpp读入结构性数组 文本查询程序 文本查询程序本版1 Cpp读入结构性数组 #include<sstream> #include<iostream> #includ ...
- C++智指针之——boost::intrusive_ptr,一种与shared_ptr、unique_ptr截然不同的智能指针
智能指针boost::shared_ptr/std::shared_ptr使用了和数据无关的引用计数,在使用shared_ptr之前,必须要搞清楚资源的所有权和资源的使用权这两个问题(详见<C+ ...
- C++智能指针中unique_ptr部分内容的讲解
参考链接 std::unique_ptr 介绍 定义位于头文件<memory> std::unique_ptr 是通过指针占有并管理另一对象,并在 unique_ptr 离开作用域时释放该 ...
- Effective Modern C++ 第四章,C++智能指针
智能指针 Smart Pointer 一些说明: C++98中有智能指针std::auto_ptr,在C++11中,出现std::unique_ptr.std::unique_ptr包含了std::a ...
- 学习笔记(02):3华为工程师 ,带你实战C++(2018版)-02仿函数与智能指针的自实现...
立即学习:https://edu.csdn.net/course/play/9122/189959?utm_source=blogtoedu Day7 仿函数和智能指针
- C++ 智能指针详解
智能指针内容很多,重点是基本用法. #include <boost/shared_ptr.hpp> class CBase: public boost::enable_shared_fro ...
- 现代C++之手写智能指针
现代C++之手写智能指针 0.回顾 所有代码还是放在仓库里面,欢迎star! https://github.com/Light-City/CPlusPlusThings 前面一节编写了一个RAII的例 ...
最新文章
- Exchange Server 2013 安装完成后配置外部URL
- SpringCloud配置中心-Config
- 在Labview中调用M文件(Matlab文件)
- 空间平滑滤波器(上)
- Ubuntu20.04 远程桌面共享vnc
- mysql命令行导入url_Mysql 导入数据,推荐Source命令,太快了
- 关于CXF大文件的传输问题
- LintCode 生成括号
- cmake中添加引用动态链接_C# 添加、编辑、删除PPT中的超链接
- hadoop-执行mapreduce时主机名非法的处理
- codeplex http://metrotoolkit.codeplex.com/
- 国二java好过还是office好过,计算机二级考哪一个科目比较容易过,Ms office 较为简单实用...
- 计算log以二为底的x用计算机,计算器上怎么按出log以2为底的数
- kappa一致性检验教程_一致性检验的几种方式--ICC、kappa、weighted kappa、Kendall
- android 11.0 12.0去掉前置摄像头闪光灯功能
- Android发送edp服务器,Android开发之基于OneNET平台的EDP传输协议的使用
- thawte,globalsign,alphassl,rapidssl,geotrust,digicert证书品牌的对照
- 魔兽世界诞生记(下)
- 写推文满一年,分享 5 个小编常用的写作软件
- Facebook名人软件Mentions有了Android版本
热门文章
- 智能车速度控制器设计
- Python--从基础到面向对象全过程笔记
- C++:vector应用
- 花西子成美妆爆款生成机,店宝宝:它是怎么做增长的?
- 中国石油大学(北京)-《 渗流力学》第二阶段在线作业
- 修改鼠标双击判断时间间隔
- DLL/EXE查看工具Dumpbin
- 人机交互学习-7 可视化设计
- 【转】Unity3D播放器插件研究
- 【每日随笔】毕业论文答辩 ④ ( 答辩陈述 | 自我介绍 | 论文题目 | 论文内容 | 研究背景 | 文件综述 | 研究内容 | 研究结论 | 总结与展望 )