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

  1. [3] 智能指针std::auto_ptr

    [1]std::auto_ptr 对于编译器来说,智能指针实质是一个栈对象,而并非指针类型. 智能指针通过构造函数获取堆内存的管理所有权,而在其生命期结束时,再通过析构函数释放由它所管理的堆内存. 所 ...

  2. 【C++深入浅出】智能指针之auto_ptr学习

    起:  C++98标准加入auto_ptr,即智能指针,C++11加入shared_ptr和weak_ptr两种智能指针,先从auto_ptr的定义学习一下auto_ptr的用法. template& ...

  3. 智能指针之auto_ptr和scoped_ptr

    部分参考地址https://blog.csdn.net/yanglingwell/article/details/56011576 auto_ptr是c++标准库里的智能指针,但是具有以下几个明显的缺 ...

  4. C++ 智能指针 std::auto_ptr 分析

    背景介绍: RAll机制 定义一个类来封装资源的分配和释放,在构造函数中完成资源的分配和初始化,在析构函数中完成资源的清理,从而保证资源的正确初始化和清理 ps:智能指针就是RAll机制的一种应用,智 ...

  5. 智能指针shared_ptr, auto_ptr, scoped_ptr, weak_ptr总结

    看这里: http://blog.csdn.net/lollipop_jin/article/details/8499530 shared_ptr可以多线程同时读,但是涉及到写,需要加锁. share ...

  6. auto_ptr,shared_ptr 智能指针的使用

    Q: 那个auto_ptr是什么东东啊?为什么没有auto_array?A: 哦,auto_ptr是一个很简单的资源封装类,是在<memory>头文件中定义的.它使用“资源分配即初始化”技 ...

  7. C++ 智能指针Auto_PTR 分析

    C++的动态内存的分配与释放是个挺折磨人的事情,尤其异常分支复杂时(比如一堆try catch中,各catch里需要做delete 掉相关的堆上分配的内存),极有可能产生内存泄露的情况.C++中提供了 ...

  8. 32.智能指针auto_ptr

    #include <iostream> #include <memory> #include <string> #include <vector> us ...

  9. C++智能指针简单剖析

    导读 最近在补看<C++ Primer Plus>第六版,这的确是本好书,其中关于智能指针的章节解析的非常清晰,一解我以前的多处困惑.C++面试过程中,很多面试官都喜欢问智能指针相关的问题 ...

随机推荐

  1. Spring Security 入门(1-3-1)Spring Security - http元素 - 默认登录和登录定制

    登录表单配置 - http 元素下的 form-login 元素是用来定义表单登录信息的.当我们什么属性都不指定的时候 Spring Security 会为我们生成一个默认的登录页面. 如果不想使用默 ...

  2. gradle入门(1-4)多项目构建实战

    一.多项目构建 1.多项目构建概念 尽管我们可以仅使用单个组件来创建可工作的应用程序,但有时候更广泛的做法是将应用程序划分为多个更小的模块. 因为这是一个非常普遍的需求,因此每个成熟的构建工具都必须支 ...

  3. maven快速下载jar镜像

    <!--国内镜像--><mirror>  <id>CN</id>  <name>OSChina Central</name>  ...

  4. IDE-Android Studio 导入Ecplise项目不改变结构

    Android Studio 导入 Ecplise创建的android 项目  无导入 不修改目录结构 首先,Ecplise 原有目录结构创建的android项目一枚 Sept 1 .  打开项目 S ...

  5. powerdesigner将name的名字赋给comment

    1 PowerDesigner中批量根据对象的name生成comment的脚本 执行方法:Open PDM -- Tools -- Execute Commands -- Run Script Vb ...

  6. Linux磁盘分区-rpm-yum

    一.磁盘分区 1.开启Linux系统前添加一块大小为15G的SCSI硬盘 2.开启系统,右击桌面,打开终端 3.为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分区 ...

  7. MIT许可证

    MIT许可证(The MIT License)是许多软件授权条款中,被广泛使用的其中一种.与其他常见的软件授权条款(如GPL.LGPL.BSD)相比,MIT是相对宽松的软件授权条款. MIT 许可证几 ...

  8. 优易软件-关于click事件在苹果手机失效的问题

    因为是动态添加的内容,所以想要使用click事件,需要给他用on绑定一下: $(document).on("click",".next_button",func ...

  9. C++的三大特性:封装、继承和多态性的详解

    封装 所谓封装就是将某些东西包装盒隐藏起来,让外界无法直接使用,只能通过某些特定的方式才能访问.封装的目的是增强安全性和简化编程,使用者不必了解具体的实现细节,而只是通过外部接口以及特定的访问权限来使 ...

  10. QT-第一个程序 Hello QT , 以及QT creator介绍

    第一个程序 - Hello QT 首先写main.cpp: #include <QApplication> #include <QMainWindow> #include &l ...