智能指针之 auto_ptr
C++的auto_ptr所做的事情,就是动态分配对象以及当对象不再需要时自动执行清理,该智能指针在C++11中已经被弃用,转而由unique_ptr替代,那这次使用和实现,就具体讲一下auto_ptr被弃用的原因,(编译平台:Linux centos 7.0 编译器:gcc 4.8.5 )
首先使用std::auto_ptr时,需要#include <memory>头文件,具体使用代码如下(文件名:test_ptr.cpp):
#include <memory>
#include <iostream> using namespace std; class Test
{
public:
Test()
{
cout << "construct.." << endl;
} ~Test()
{
cout << "destruct.." << endl;
}
}; void test()
{ } int main()
{
Test* p = new Test();
auto_ptr<Test> ap(p); return 0;
}
执行上述代码,我们可以看到,程序结束时,在堆上申请的对象,自动执行了析构函数,将内存释放了
[root@localhost code]# g++ -g -o autop test_ptr.cpp
[root@localhost code]# ./autop
construct..
destruct..
[root@localhost code]#
具体实现代码如下,构造函数只实现了初始化构造和拷贝构造:
#include <iostream> using namespace std; template<typename T>
class auto_pt
{
public:
explicit auto_pt(T* p = NULL):m_ptr(p)
{
p = NULL;
cout << "auto_ptr construct" << endl;
} auto_pt(auto_pt& autoPtr):m_ptr(autoPtr.m_ptr)
{
autoPtr.m_ptr = NULL;
cout << "copy auto_ptr construct" << endl;
} auto_pt& operator = (auto_pt& p)
{
if(this != &p)
{
if(m_ptr != NULL)
{
delete m_ptr;
m_ptr = p.m_ptr;
p.m_ptr = NULL;
}
} return *this;
} ~auto_pt()
{
if(m_ptr != NULL)
{
cout << "auto_ptr destruct" << endl;
delete m_ptr;
m_ptr = NULL;
} } T* Get()
{
return m_ptr;
} T& operator*()
{
return *m_ptr;
} T* operator->()
{
return m_ptr;
} private:
T* m_ptr;
}; class Test
{
public:
Test()
{
cout << "construct.." << endl;
} ~Test()
{
cout << "destruct.." << endl;
} void method()
{
cout << "welcome Test.." << endl;
}
}; void f(auto_pt<Test>ap)
{
cout << "funtion f :";
ap->method();
} int main()
{
//baseic test
Test* p = new Test();
cout << "address0 [%p]" << p << endl;
auto_pt<Test> ap(p); cout << "address1 [%p]" << ap.Get()<< endl;
cout << "address2 [%p]" << &ap << endl;
cout << "address3 [%p]" << &(*ap) << endl; ap.Get()->method();
(*ap).method();
ap->method(); return ;
}
打印结果:
[root@localhost code]# g++ -o autop_test test.cpp
[root@localhost code]# ./autop_test
construct..
address0 [%p]0xb77010
auto_ptr construct
address1 [%p]0xb77010
address2 [%p]0x7ffe8b25f510
address3 [%p]0xb77010
welcome Test..
welcome Test..
welcome Test..
auto_ptr destruct
destruct..
[root@localhost code]#
大概实现就是这样,基本和标准库差不多,除了另外两种类型的构造函数没有加进去
那在我们使用及实现的过程中,发现这个auto_ptr在使用过程中会有如下风险,因此在C++11中已经不再使用,那在我们开发过程中,也最好不要再使用
1. 两个auto_ptr指向同一块内存,造成多次释放
//if 2 object point one address, application will die
Test* p1 = new Test(); auto_pt<Test> ap1(p1);
auto_pt<Test> ap2(p1);
2. 复制完成后,会将复制的对象置空,因此不能继续使用
int*p=new int();
auto_pt<int>ap1(p);
auto_pt<int>ap2=ap1;
(*ap1).method();//错误,此时ap1只剩一个null指针在手了
3. 函数形参使用值传递,会发生拷贝操作,导致ap1对象权限获取不到了
void f(auto_pt<int>ap)
{
(*ap).method();
} auto_pt<int>ap1(new int());
f(ap1);
(*ap1).method();;//错误,经过f(ap1)函数调用,ap1已经不再拥有任何对象了。
智能指针之 auto_ptr的更多相关文章
- [3] 智能指针std::auto_ptr
[1]std::auto_ptr 对于编译器来说,智能指针实质是一个栈对象,而并非指针类型. 智能指针通过构造函数获取堆内存的管理所有权,而在其生命期结束时,再通过析构函数释放由它所管理的堆内存. 所 ...
- 【C++深入浅出】智能指针之auto_ptr学习
起: C++98标准加入auto_ptr,即智能指针,C++11加入shared_ptr和weak_ptr两种智能指针,先从auto_ptr的定义学习一下auto_ptr的用法. template& ...
- 智能指针之auto_ptr和scoped_ptr
部分参考地址https://blog.csdn.net/yanglingwell/article/details/56011576 auto_ptr是c++标准库里的智能指针,但是具有以下几个明显的缺 ...
- C++ 智能指针 std::auto_ptr 分析
背景介绍: RAll机制 定义一个类来封装资源的分配和释放,在构造函数中完成资源的分配和初始化,在析构函数中完成资源的清理,从而保证资源的正确初始化和清理 ps:智能指针就是RAll机制的一种应用,智 ...
- 智能指针shared_ptr, auto_ptr, scoped_ptr, weak_ptr总结
看这里: http://blog.csdn.net/lollipop_jin/article/details/8499530 shared_ptr可以多线程同时读,但是涉及到写,需要加锁. share ...
- auto_ptr,shared_ptr 智能指针的使用
Q: 那个auto_ptr是什么东东啊?为什么没有auto_array?A: 哦,auto_ptr是一个很简单的资源封装类,是在<memory>头文件中定义的.它使用“资源分配即初始化”技 ...
- C++ 智能指针Auto_PTR 分析
C++的动态内存的分配与释放是个挺折磨人的事情,尤其异常分支复杂时(比如一堆try catch中,各catch里需要做delete 掉相关的堆上分配的内存),极有可能产生内存泄露的情况.C++中提供了 ...
- 32.智能指针auto_ptr
#include <iostream> #include <memory> #include <string> #include <vector> us ...
- C++智能指针简单剖析
导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...
随机推荐
- SUN平台服务器光纤共享存储互斥失败如何恢复数据?
服务器数据恢复故障描述: 服务器最初的设计思路为将两台SPARC SOLARIS系统通过光纤交换机共享同一存储作为CLUSTER使用,正常情况下A服务器工作,当A服务器发生故障宕机后即可将其关机然后开 ...
- jquery 表双击某行时,取出某行中各列的数值.
<script> $(function () { $("tr").dblclick(function () { var txt = $("table tr ...
- 从PRISM开始学WPF(九)交互(完结)
0x07交互 Notification xaml: <Window x:Class="UsingPopupWindowAction.Views.MainWindow" xml ...
- Mysql编译安装详解
wget http://mirrors.cnnic.cn/apache/httpd/mysql-5.5.20.tar.gz root@Mysql-server ~]# yum install -y c ...
- Mego开发文档 - 索引
Mego 开发文档 Mego 快速概述 主要特性 获取Mego 使用流程 模型 查询 保存数据 入门 Mego 快速开始 创建项目 安装Nuget包 创建连接字符串 创建模型及数据上下文(添加引用) ...
- 2018年Web前端自学路线
本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. Web前端入门的自学路线 新手入门前端,需要学习的基础内容有很多,如下. ...
- Linux--慕课学习
刚开始接触Linux,很有幸的在慕课网上看到了Peter老师的Linux入门课程,老师讲课真的式行云流水,深入浅出,循循善诱,层层递进. 老师分享的都是自己多年来总结的经验.看完之后也学到了很多东西. ...
- django Form组件 上传文件
上传文件 注意:FORM表单提交文件要有一个参数enctype="multipart/form-data" 普通上传: urls: url(r'^f1/',views.f1), u ...
- XSS和CSRF的理解
声明:转自 http://www.cnblogs.com/hyddd/archive/2009/04/09/1432744.html XSS攻击:跨站脚本攻击(Cross Site Scripting ...
- Hive:有表A与表B进行inner join,如果A分组内包含有数据,使用A,否则使用B分组下的数据
tommyduan_fingerlib 指纹库 栅格小区级别数据tommyduan_mr_grid_cell_result_all 统计 栅格小区级别数据业务:以tommyduan_mr_grid_c ...