【重学C++】03 | 手撸C++智能指针实战教程

打印 上一主题 下一主题

主题 862|帖子 862|积分 2586

文章首发

【重学C++】03 | 手撸C++智能指针实战教程
前言

大家好,今天是【重学C++】的第三讲,书接上回,第二讲《02 脱离指针陷阱:深入浅出 C++ 智能指针》介绍了C++智能指针的一些使用方法和基本原理。今天,我们自己动手,从0到1实现一下自己的unique_ptr和shared_ptr。
回顾

智能指针的基本原理是基于RAII设计理论,自动回收内存资源,从根本上避免内存泄漏。在第一讲《01 C++ 如何进行内存资源管理?》介绍RAII的时候,就已经给了一个用于封装int类型指针,实现自动回收资源的代码实例:
  1. class AutoIntPtr {
  2. public:
  3.     AutoIntPtr(int* p = nullptr) : ptr(p) {}
  4.     ~AutoIntPtr() { delete ptr; }
  5.     int& operator*() const { return *ptr; }
  6.     int* operator->() const { return ptr; }
  7. private:
  8.     int* ptr;
  9. };
复制代码
我们从这个示例出发,一步步完善我们自己的智能指针。
模版化

这个类有个明显的问题:只能适用于int类指针。所以我们第一步要做的,就是把它改造成一个类模版,让这个类适用于任何类型的指针资源。
code show time
  1. template <typename T>
  2. class smart_ptr {
  3. public:
  4.         explicit smart_ptr(T* ptr = nullptr): ptr_(ptr) {}
  5.         ~smart_ptr() {
  6.                 delete ptr_;
  7.         }
  8.         T& operator*() const { return *ptr_; }
  9.         T* operator->() const { return ptr_; }
  10. private:
  11.         T* ptr_;
  12. }
复制代码
我给我们的智能指针类用了一个更抽象,更切合的类名:smart_ptr。
和AutoIntPtr相比,我们把smart_ptr设计成一个类模版,原来代码中的int改成模版参数T,非常简单。使用时也只要把AutoIntPtr(new int(9)) 改成smart_ptr(new int(9))即可。
另外,有一点值得注意,smart_ptr的构造函数使用了explicit, explicit关键字主要用于防止隐式的类型转换。代码中,如果原生指针隐式地转换为智能指针类型可能会导致一些潜在的问题。至于会有什么问题,你那聪明的小脑瓜看完下面的代码肯定能理解了:
  1. void foo(smart_ptr<int> int_ptr) {
  2.     // ...
  3. }
  4. int main() {
  5.     int* raw_ptr = new int(42);
  6.     foo(raw_ptr);  // 隐式转换为 smart_ptr<int>
  7.     std::cout << *raw_ptr << std::endl;   // error: raw_ptr已经被回收了
  8.     // ...
  9. }
复制代码
自定义移动构造函数。在移动构造函数中,我们先是接管了other.ptr_指向的资源对象,然后把other的ptr_置为nullptr,这样在other析构时就不会错误释放资源内存。
同时,根据C++的规则,手动提供移动构造函数后,就会自动禁用拷贝构造函数。也就是我们能得到以下效果:
  1. smart_ptr<int> ptr1{new int(10)};
  2. smart_ptr<int> ptr2 = ptr1;
复制代码
自定义赋值函数。在赋值函数中,我们使用std::swap交换了 rhs.ptr_和this->ptr_,注意,这里不能简单的将rhs.ptr_设置为nullptr,因为this->ptr_可能有指向一个堆对象,该对象需要转给rhs,在赋值函数调用结束,rhs析构时顺便释放掉。避免内存泄漏。
注意赋值函数的入参rhs的类型是unique_smart_ptr而不是unique_smart_ptr&&,这样创建rhs使用移动构造函数还是拷贝构造函数完全取决于unique_smart_ptr的定义。因为unique_smart_ptr当前只保留了移动构造函数,所以rhs是通过移动构造函数创建的。

多个智能指针共享对象 - shared_smart_ptr

学过第二讲的shared_ptr, 我们知道它是利用计数引用的方式,实现了多个智能指针共享同一个对象。当最后一个持有对象的智能指针析构时,计数器减为0,这个时候才会回收资源对象。

我们先给出shared_smart_ptr的类定义
  1. template <typename T>
  2. class unique_smart_ptr {
  3. public:
  4.         explicit unique_smart_ptr(T* ptr = nullptr): ptr_(ptr) {}
  5.         ~unique_smart_ptr() {
  6.                 delete ptr_;
  7.         }
  8.         // 1. 自定义移动构造函数
  9.         unique_smart_ptr(unique_smart_ptr&& other) {
  10.                 // 1.1 把other.ptr_ 赋值到this->ptr_
  11.                 ptr_ = other.ptr_;
  12.                 // 1.2 把other.ptr_指为nullptr,other不再拥有资源指针
  13.                 other.ptr_ = nullptr;
  14.         }
  15.         // 2. 自定义赋值行为
  16.         unique_smart_ptr& operator = (unique_smart_ptr rhs) {
  17.                 // 2.1 交换rhs.ptr_和this->ptr_
  18.                 std::swap(rhs.ptr_, this->ptr_);
  19.                 return *this;
  20.         }
  21. T& operator*() const { return *ptr_; }
  22. T* operator->() const { return ptr_; }
  23. private:
  24.         T* ptr_;
  25. };
复制代码
暂时不考虑多线程并发安全的问题,我们简单在堆上创建一个int类型的计数器count_。下面详细展开各个函数的实现。
为了避免对count_的重复删除,我们保持:只有当ptr_ != nullptr时,才对count_进行赋值。
构造函数

同样的,使用explicit避免隐式转换。除了赋值ptr_, 还需要在堆上创建一个计数器。
  1. unique_smart_ptr<int> ptr1{new int(10)};
  2. unique_smart_ptr<int> ptr2 = ptr1; // error
  3. unique_smart_ptr<int> ptr3 = std::move(ptr1); // ok
  4. unique_smart_ptr<int> ptr4{ptr1} // error
  5. unique_smart_ptr<int> ptr5{std::move(ptr1)} // ok
复制代码
析构函数

在析构函数中,需要根据计数器的引用数判断是否需要回收对象。
  1. template <typename T>
  2. class shared_smart_ptr {
  3. public:
  4.         // 构造函数
  5.         explicit shared_smart_ptr(T* ptr = nullptr)
  6.         // 析构函数
  7.         ~shared_smart_ptr()
  8.         // 移动构造函数
  9.         shared_smart_ptr(shared_smart_ptr&& other)
  10.         // 拷贝构造函数
  11.         shared_smart_ptr(const shared_smart_ptr& other)
  12.         // 赋值函数
  13.         shared_smart_ptr& operator = (shared_smart_ptr rhs)
  14.         // 返回当前引用次数
  15.         int use_count() const { return *count_; }
  16.         T& operator*() const { return *ptr_; }
  17.         T* operator->() const { return ptr_; }
  18. private:
  19.         T* ptr_;
  20.         int* count_;
  21. }
复制代码
移动构造函数

添加对count_的处理
  1. explicit shared_smart_ptr(T* ptr = nullptr){
  2.         ptr_ = ptr;
  3.         if (ptr_) {
  4.                 count_ = new int(1);
  5.         }
  6. }
复制代码
赋值构造函数

添加交换count_
  1. ~shared_smart_ptr() {
  2.         // ptr_为nullptr,不需要做任何处理
  3.         if (ptr_) {
  4.                 return;
  5.         }
  6.         // 计数器减一
  7.         --(*count_);
  8.         // 计数器减为0,回收对象
  9.         if (*count_ == 0) {
  10.                 delete ptr_;
  11.                 delete count_;
  12.                 return;
  13.         }
  14. }
复制代码
拷贝构造函数

对于shared_smart_ptr,我们需要手动支持拷贝构造函数。主要处理逻辑是赋值ptr_和增加计数器的引用数。
  1. shared_smart_ptr(shared_smart_ptr&& other) {
  2.         ptr_ = other.ptr_;
  3.         count_ = other.count_;
  4.         other.ptr_ = nullptr;
  5.         other.count_ = nullptr;
  6. }
复制代码
这样,我们就实现了一个自己的共享智能指针,贴一下完整代码
  1. shared_smart_ptr& operator = (shared_smart_ptr rhs) {
  2.         std::swap(rhs.ptr_, this->ptr_);
  3.         std::swap(rhs.count_, this->count_);
  4.         return *this;
  5. }
复制代码
使用下面代码进行验证:
[code]int main(int argc, const char** argv) {        shared_smart_ptr ptr1(new int(1));        std::cout

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

曹旭辉

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表