1 shared_ptr的实现原理

智能指针的一种通用实现技术是使用引用计数。智能指针类将一个计数器与智能指针指向的对象相关联,用来记录有多少个智能指针指向相同的对象,并在恰当的时候释放对象。

每次创建类的新对象时,初始化指针并将引用计数置为1;当对象作为另一对象的副本而创建时,引用计数加1;对一个对象进行赋值时,赋值操作符减少左操作数所指对象的引用计数(如果引用计数为减至0,则删除对象),并增加右操作数所指对象的引用计数;调用析构函数时,构造函数减少引用计数(如果引用计数减至0,则删除基础对象)。

由图中可以看到,实际上引用计数、自定义销毁等都不是直接存储在shared_ptr中,而是通过一个指针指向的一个控制块存储的,控制块是动态分配的内存 。

2 代码简单实现

#include <iostream>
#include <memory>
#include <string>
#include <assert.h>

using namespace std;

// 使用一个类来管理引用计数
class RefCounter {
public:
    RefCounter(long val) : count(val) {};
    void add_ref() { // c++标准库中的操作是一个原子操作,且要保证线程安全(如加锁操作)
        ++count;
    }
    const long reduce_ref() {
        return --count;
    }
    long get_use_count() const noexcept {
        return count;
    }
private:
    long count;
};

// 自定义一个模板类的智能指针
template<typename Element_Type>
class SmartPointer {
public:
    // 使用一般指针类型,初始化一个智能指针
    SmartPointer(Element_Type* ptr = nullptr) : mp(ptr) {
        if (mp) {
            mCount = new RefCounter(1);
        }
        else {
            mCount = new RefCounter(0);
        }
    }
    // 拷贝构造函数,使用一个已有的智能指针,初始化一个新的智能指针
    // 新的智能指针拥有了原来的智能指针管理的指针地址 和 引用计数数值,同时,新的智能指针引用计数加1(当然,原来的指针智能指针引用计数不会发生变化)
    SmartPointer(const SmartPointer& ptr) {
        mp = ptr.mp;
        mCount = ptr.mCount;
        mCount->add_ref();
    }
    // 赋值函数:当前智能指针引用计数减1,若减到0就销毁原来指向的内存。
    // 举例: sp2 = sp3
    // 否则使用新的智能指针sp3 的属性覆盖当前智能指针sp2 的属性,并使引用计数加1
    // (加的既是 当前智能指针sp2 的引用计数, 同时又是新的智能指针sp3的引用计数, 因为它们的引用计数是同一地址对应的变量 count)
    SmartPointer& operator=(const SmartPointer& ptr) {
        if (mp == ptr.mp) {
            return *this;
        }

        if (mp) {
            if (mCount->reduce_ref() == 0) {
                delete mp;
                delete mCount;
            }
        }

        mp = ptr.mp; //因为使用引用计数,所以直接用一个指针的值覆盖另一个指针的值,只要引用计数不减到0就先不必销毁原有的内存,也不必担心内存会泄漏(地址未归还系统)
        mCount = ptr.mCount; // (加的既是 当前智能指针sp2 的引用计数, 同时又是新的智能指针sp3的引用计数, 因为它们的引用计数是同一地址对应的变量 count)
        mCount->add_ref();
        return *this;
    }

    // 解引用 dereference // 返回原始指针指向的变量(实例)的引用
    Element_Type& operator*() {
         assert(mp != nullptr);
         return *mp;
    }

    Element_Type* operator->() {
        assert(mp != nullptr);
        return mp;
    }

    operator bool() const {
        return mp;
    }

    // 每析构一次智能指针就减少一次引用计数
    ~SmartPointer() {
        if (mCount->reduce_ref() == 0) {
            delete mp;
            delete mCount;
        }
    }

    size_t use_count() {
        return mCount->get_use_count();
    }
private:
    Element_Type* mp;
    RefCounter*   mCount;
};

// 简单定义一个使用智能指针管理内存的用户类
class Person {
public:
    Person(int age, string name) : mAge(age), mName(name) {};
    string info() {
        string info = "Name: " + mName + ", Age:" + std::to_string(mAge);
        return info;
    }
private:
    int mAge;
    string mName;
};

int main() {
    SmartPointer<Person> sp1(new Person(10, "zhangsan"));
    SmartPointer<Person> sp2(sp1); // 使用智能指针拷贝构造一个新的智能指针,则 sp2 sp3 引用计数都+1
    SmartPointer<Person> sp3(new Person(20, "Lisi"));

    std::cout << sp1.use_count() << ", " << sp1->info() << std::endl; // 2, Name: zhangsan, Age:10
    std::cout << sp2.use_count() << ", " << sp2->info() << std::endl; // 2, Name: zhangsan, Age:10
    std::cout << sp3.use_count() << ", " << sp3->info() << std::endl; // 1, Name: Lisi, Age:20

    sp2 = sp3;  // 使用智能赋值一个智能指针,被赋值的智能sp2原来对应的指针的引用计数-1,用于赋值的智能指针引用计数+1

    std::cout << sp1.use_count() << ", " << sp1->info() << std::endl; // 1, Name: zhangsan, Age:10
    std::cout << sp2.use_count() << ", " << sp2->info() << std::endl; // 2, Name: Lisi, Age:20
    std::cout << sp3.use_count() << ", " << sp3->info() << std::endl; // 2, Name: Lisi, Age:20

    return 0;
}

// 将上述main函数中 SmartPointer 改成 shared_ptr std::cout输出的结果是一样的。

参考

C++11—智能指针详解及实现

用c++简单实现智能指针

如何实现一个简单的智能指针