智能指针auto_ptr、unique_ptr、shared_ptr区别

转自:https://zhuanlan.zhihu.com/p/63890916

模板auto_ptr是C++98提供的解决方案,C++11已摒弃。

模板unique_ptr、shared_ptr是C++11提供的解决方案.

为什么要摒弃auto_ptr呢?

先来看下面的赋值语句:

auto_ptr<string> ps(new string("I am a boy."));
auto_ptr<stirng> vocation;
vocation = ps;

上述赋值语句将完成什么工作呢?如果ps和vocation是常规指针,则两个指针将指向同一个string对象。这是不能接受的,因为程序将试图删除同一个对象两次--一次是ps过期时,一次是vocation过期时。要避免这种问题,方法有多种。

  • 定义赋值运算符,使之执行深赋值。这样两个指针将指向不同的对象,其中的一个对象是另一个对象的副本。
  • 建立所有权(ownership)概念,对于特定的对象,只能有一个智能指针可拥有它,这样只能拥有对象的智能指针的构造函数会删除该对象。然后,让赋值操作转让所有权。这就是用于auto_ptr和unique_ptr的策略,但unique_ptr的策略更严格。
  • 创建智能更高的指针,跟踪引用特定对象的智能指针数。这称为引用计数(reference counting)。例如,赋值时,计数将加1,而指针过期时,计数将减1。仅当最后一个指针过期时,才调用delete。这是shared_ptr采用的策略。

每种方法都有其用途,

1 下面是一个不适合使用auto_ptr的示例:

#include <iostream>
#include <string>
#include <memory>

using namespace std;

int main()
{
    auto_ptr<string> films[5] =
    {
        auto_ptr<string> (new string("one")),
        auto_ptr<string> (new string("two")),
        auto_ptr<string> (new string("three")),
        auto_ptr<string> (new string("four")),
        auto_ptr<string> (new string("five"))
    };
    auto_ptr<string> pwin;
    pwin = films[2];  // films[2] lose ownership

    cout << "films data is: " << endl;
    for(auto_ptr<string> s : films)
        cout << *s << endl;
    cout << "pwin: " << *pwin << endl;

    return 0;
}

下面是该程序的输出:

films data is:
one
two

Process returned -1073741819 (0xC0000005)   execution time : 1.659 s
Press any key to continue.

错误的使用auto_ptr可能导致问题(这种代码的行为是不确定的,其行为可能随系统而异)。这里的问题在于,下面的语句将所有权从films[2]转让给pwin:

pwin = films[2];  // films[2] lose ownership

这导致films[2]不再引用该字符串。在auto_ptr放弃对象的所有权后,边可能使用它来访问该对象。当程序打印films[2]指向的字符串时,却发现这是一个空指针,因此发生错误。

2 如果使用shared_ptr替换auto_ptr,则程序将正常运行。

示例代码:

#include <iostream>
#include <string>
#include <memory>

using namespace std;

int main()
{
    shared_ptr<string> films[5] =
    {
        shared_ptr<string> (new string("one")),
        shared_ptr<string> (new string("two")),
        shared_ptr<string> (new string("three")),
        shared_ptr<string> (new string("four")),
        shared_ptr<string> (new string("five"))
    };
    shared_ptr<string> pwin;
    pwin = films[2];

    cout << "films data is: " << endl;
    for(shared_ptr<string> s : films)
        cout << *s << endl;
    cout << "pwin: " << *pwin << endl;

    return 0;
}

其输出如下:

films data is:
one
two
three
four
five
pwin: three

这次pwin和films[2]指向同一个对象,而引用计数从1增加到2。在程序末尾,后声明的pwin首先调用其析构函数,该析构函数将引用计数降低到1。然后,shared_ptr数组的成员被释放,对films[2]调用析构函数时,将引用计数降低到0,并释放以前分配的空间。

3 如果使用unique_ptr替换auto_ptr

#include <iostream>
#include <string>
#include <memory>

using namespace std;

int main()
{
    unique_ptr<string> films[5] =
    {
        unique_ptr<string> (new string("one")),
        unique_ptr<string> (new string("two")),
        unique_ptr<string> (new string("three")),
        unique_ptr<string> (new string("four")),
        unique_ptr<string> (new string("five"))
    };
    unique_ptr<string> pwin;
    pwin = films[2];

    cout << "films data is: " << endl;
    for(unique_ptr<string> s : films)
        cout << *s << endl;
    cout << "pwin: " << *pwin << endl;

    return 0;
}

则程序将在下述代码行出现编译错误。

pwin = films[2];  // films[2] lose ownership

参考

《C++Primer Plus(第6版)中文版》 第16章P671

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值