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. nyoj 擅长排列的小名II

    擅长排列的小明 II 时间限制:1000 ms  |           内存限制:65535 KB 难度:3   描述 小明十分聪明,而且十分擅长排列计算. 有一天小明心血来潮想考考你,他给了你一个 ...

  2. Python之旅.第三章.函数3.29

    一.无参装饰器 1 开放封闭原则 软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案: 能够在不修改一个功能源代码以及调用方式的前提 ...

  3. 腾讯云服务器上安装phstudy和lnmp

    phpstudy的安装:wget -c http://lamp.phpstudy.net/phpstudy.bin chmod +x phpstudy.bin #权限设置./phpstudy.bin ...

  4. 在WebStorm中启动Angular项目

    点击配置 创建 选择命令 package.json 运行 查看运行结果

  5. ssl双向认证

    ssl双向认证 一.背景知识 1.名词解释 ca.key: 根证书的私钥 , ca.crt: 根证书的签名证书 server.key, server.crt client.key, client.cr ...

  6. 修改了SpringBoot的主类名称后,gradle build报错的解决办法

    Unable to find a single main class from the following candidates [*.*Application]

  7. SpringBoot应用的属性管理

    一.properties 配置文件 1.src/main/application.properties spring.profiles.active=dev spring.application.na ...

  8. angular2 学习笔记 ( angular cli & npm version manage npm 版本管理 )

    更新 : 2017-05-05 现在流行 Yarn ! 它是 facebook google 推出的东西. 算是补助 npm 做的不够好的地方. 源码依然是发布去 npm,只是下载接口换掉罢了哦. n ...

  9. SpringCloud的服务消费者 (一):(rest+ribbon)访问注册的微服务

    采用Ribbon或Feign方式访问注册到EurekaServer中的微服务.1.Ribbon实现了客户端负载均衡,Feign底层调用Ribbon2.注册在EurekaServer中的微服务api,不 ...

  10. Python 编写登录接口

    流程图: 源码: