Effective C++笔记之二十:smart pointer的实现原理

本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针。

一.使用智能指针的缘由

1. 考虑下边的简单代码:

int main()
{
     int *ptr = new int(0);
     return 0;
}

就如上边程序,我们有可能一不小心就忘了释放掉已不再使用的内存,从而导致资源泄漏(resoure leak,在这里也就是内存泄漏)。
2. 考虑另一简单代码:

int main()
{
     int *ptr = new int(0);
     delete ptr;
     return 0;
}

我们可能会心想,这下程序应该没问题了?可实际上程序还是有问题。上边程序虽然最后释放了申请的内存,但ptr会变成空悬指针(dangling pointer,也就是野指针)。空悬指针不同于空指针(nullptr),它会指向“垃圾”内存,给程序带去诸多隐患(如我们无法用if语句来判断野指针)。
上述程序在我们释放完内存后要将ptr置为空,即:

ptr = nullptr;

除了上边考虑到的两个问题,上边程序还存在另一问题:如果内存申请不成功,new会抛出异常,而我们却什么都没有做!所以对这程序我们还得继续改进(也可用try...catch...):

#include <iostream>
using namespace std;

int main()
{
    int *ptr = new(nothrow) int(0);
    if(!ptr)
    {
        cout << "new fails."
        return 0;
    }
    delete ptr;
    ptr = nullptr;
    return 0;
}

3. 考虑最后一段简单代码:

#include <iostream>
using namespace std;

int main()
{
    int *ptr = new(nothrow) int(0);
    if(!ptr)
    {
        cout << "new fails."
        return 0;
    }
    // 假定hasException函数原型是 bool hasException()
    if (hasException())
        throw exception();

    delete ptr;
    ptr = nullptr;
    return 0;
}

当我们的程序运行到“if(hasException())”处且“hasException()”为真,那程序将会抛出一个异常,最终导致程序终止,而已申请的内存并没有释放掉。
当然,我们可以在“hasException()”为真时释放内存:

// 假定hasException函数原型是 bool hasException()
if (hasException())
{
        delete ptr;
        ptr = nullptr;
        throw exception();
}

但我们并不总会想到这么做。而且,这样子做也显得麻烦,不够人性化。
如果,我们使用智能指针,上边的问题我们都不用再考虑,因为它都已经帮我们考虑到了。
因此,我们使用智能指针的原因至少有以下三点:
1)智能指针能够帮助我们处理资源泄露问题;
2)它也能够帮我们处理空悬指针的问题;
3)它还能够帮我们处理比较隐晦的由异常造成的资源泄露。

二.智能指针

自C++11起,C++标准提供两大类型的智能指针:
1. shared_ptr实现共享式拥有(shared ownership)概念。多个智能指针可以指向相同对象,该对象和其相关资源会在“最后一个引用(reference)被销毁”时候释放。为了在结构复杂的情境中执行上述工作,标准库提供了weak_ptr、bad_weak_ptr和enable_shared_from_this等辅助类。
2. unique_ptr实现独占式拥有(exclusive ownership)或严格拥有(strict ownership)概念,保证同一时间内只有一个智能指针可以指向该对象。它对于避免资源泄露(resourece leak)——例如“以new创建对象后因为发生异常而忘记调用delete”——特别有用。
注:C++98中的uto_ptr在C++11中已不再建议使用。

shared_ptr

几乎每一个有分量的程序都需要“在相同时间的多处地点处理或使用对象”的能力。为此,我们必须在程序的多个地点指向(refer to)同一对象。虽然C++语言提供引用(reference)和指针(pointer),还是不够,因为我们往往必须确保当“指向对象”的最末一个引用被删除时该对象本身也被删除,毕竟对象被删除时析构函数可以要求某些操作,例如释放内存或归还资源等等。
所以我们需要“当对象再也不被使用时就被清理”的语义。shared_ptr提供了这样的共享式拥有语义。也就是说,多个shared_ptr可以共享(或说拥有)同一对象。对象的最末一个拥有者有责任销毁对象,并清理与该对象相关的所有资源
shared_ptr的目标就是,在其所指向的对象不再被使用之后(而非之前),自动释放与对象相关的资源。
下边程序摘自《C++标准库(第二版)》5.2.1节:

#include <iostream>
#include <string>
#include <vector>
#include <memory>
using namespace std;

int main(void)
{
    // two shared pointers representing two persons by their name
    shared_ptr<string> pNico(new string("nico"));
    shared_ptr<string> pJutta(new string("jutta"),
            // deleter (a lambda function)
            [](string *p)
            {
                cout << "delete " << *p << endl;
                delete p;
            }
        );

    // capitalize person names
    (*pNico)[0] = 'N';
    pJutta->replace(0, 1, "J");

    // put them multiple times in a container
    vector<shared_ptr<string>> whoMadeCoffee;
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pNico);
    whoMadeCoffee.push_back(pJutta);
    whoMadeCoffee.push_back(pNico);

    // print all elements
    for (auto ptr : whoMadeCoffee)
        cout << *ptr << " ";
    cout << endl;

    // overwrite a name again
    *pNico = "Nicolai";

    // print all elements
    for (auto ptr : whoMadeCoffee)
        cout << *ptr << " ";
    cout << endl;

    // print some internal data
    cout << "use_count: " << whoMadeCoffee[0].use_count() << endl;

    return 0;
}

程序运行结果如下:


关于程序逻辑可见下图:


关于程序的几点说明:
1)对智能指针pNico的拷贝是浅拷贝,所以当我们改变对象“Nico”的值为“Nicolai”时,指向它的指针都会指向新值。
2)指向对象“Jutta”的有四个指针:pJutta和pJutta的三份被安插到容器内的拷贝,所以上述程序输出的use_count为4。
3)shared_ptr本身提供默认内存释放器(default deleter),调用的是delete,不过只对“由new建立起来的单一对象”起作用。当然我们也可以自己定义内存释放器,就如上述程序。不过值得注意的是,默认内存释放器并不能释放数组内存空间,而是要我们自己提供内存释放器,如:

shared_ptr<int> pJutta2(new int[10],
        // deleter (a lambda function)
        [](int *p)
        {
            delete[] p;
        }
    );

或者使用为unique_ptr而提供的辅助函数作为内存释放器,其内调用delete[]:

shared_ptr<int> p(new int[10], default_delete<int[]>());

unique_ptr

unique_ptr是C++标准库自C++11起开始提供的类型。它是一种在异常发生时可帮助避免资源泄露的智能指针。一般而言,这个智能指针实现了独占式拥有概念,意味着它可确保一个对象和其相应资源同一时间只被一个指针拥有。一旦拥有者被销毁或变成空,或开始拥有另一个对象,先前拥有的那个对象就会被销毁,其任何相应资源也会被释放。
现在,本文最开头的程序就可以写成这样啦:

#include <memory>
using namespace std;

int main()
{
     unique_ptr<int> ptr(new int(0));
     return 0;
}

三.智能指针简单实现

基于引用计数的智能指针可以简单实现如下(详细解释见程序中注释):

#include <iostream>
using namespace std;

template<class T>
class SmartPtr
{
public:
    SmartPtr(T *p);
    ~SmartPtr();
    SmartPtr(const SmartPtr<T> &orig);                // 浅拷贝
    SmartPtr<T>& operator=(const SmartPtr<T> &rhs);    // 浅拷贝
private:
    T *ptr;
    // 将use_count声明成指针是为了方便对其的递增或递减操作
    int *use_count;
};

template<class T>
SmartPtr<T>::SmartPtr(T *p) : ptr(p)
{
    try
    {
        use_count = new int(1);
    }
    catch (...)
    {
        delete ptr;
        ptr = nullptr;
        use_count = nullptr;
        cout << "Allocate memory for use_count fails." << endl;
        exit(1);
    }

    cout << "Constructor is called!" << endl;
}

template<class T>
SmartPtr<T>::~SmartPtr()
{
    // 只在最后一个对象引用ptr时才释放内存
    if (--(*use_count) == 0)
    {
        delete ptr;
        delete use_count;
        ptr = nullptr;
        use_count = nullptr;
        cout << "Destructor is called!" << endl;
    }
}

template<class T>
SmartPtr<T>::SmartPtr(const SmartPtr<T> &orig)
{
    ptr = orig.ptr;
    use_count = orig.use_count;
    ++(*use_count);
    cout << "Copy constructor is called!" << endl;
}

// 重载等号函数不同于复制构造函数,即等号左边的对象可能已经指向某块内存。
// 这样,我们就得先判断左边对象指向的内存已经被引用的次数。如果次数为1,
// 表明我们可以释放这块内存;反之则不释放,由其他对象来释放。
template<class T>
SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T> &rhs)
{
    // 《C++ primer》:“这个赋值操作符在减少左操作数的使用计数之前使rhs的使用计数加1,
    // 从而防止自身赋值”而导致的提早释放内存
    ++(*rhs.use_count);

    // 将左操作数对象的使用计数减1,若该对象的使用计数减至0,则删除该对象
    if (--(*use_count) == 0)
    {
        delete ptr;
        delete use_count;
        cout << "Left side object is deleted!" << endl;
    }

    ptr = rhs.ptr;
    use_count = rhs.use_count;

    cout << "Assignment operator overloaded is called!" << endl;
    return *this;
}

测试程序如下:

#include <iostream>
#include "smartptr.h"
using namespace std;

int main()
{
    // Test Constructor and Assignment Operator Overloaded
    SmartPtr<int> p1(new int(0));
    p1 = p1;
    // Test Copy Constructor
    SmartPtr<int> p2(p1);
    // Test Assignment Operator Overloaded
    SmartPtr<int> p3(new int(1));
    p3 = p1;

    return 0;
}

测试结果如下:

 原文链接:Effective C++笔记之二十:smart pointer的实现原理-CSDN博客

  • 19
    点赞
  • 88
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草上爬

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值