智能指针

1. 概念

1.1. 普通指针的遗憾

  • 可能存在忘记释放内存
  • 可能二次释放内存
  • 可能悬空指针,即出现野指针

    1.2. 智能指针的定义

  • 智能指针(smart pointer)是个类模板,头文件为

  • 只管new内存,不用考虑内存的管理
  • 在C++11中有三种智能指针
    • unique_ptr
    • shared_ptr
    • weak_ptr
  • 所有智能指针都具有的方法

    • .operator->( )
    • .operator*( )

      2. unique_ptr

      2.1. 概念

  • 独占拥有式智能指针,只有一个unique_ptr指向某个对象,unique_ptr对象在自身生存周期结束时自动销毁所指对象。

    2.2. 方法接口

  • 无参构造方法

    1. `unique_ptr<A> ptr;`
  • 单参构造方法

    1. `unique_ptr<A> uptr(new A);`
  • 不支持拷贝构造

    1. `unique_ptr<A> up1(up); //error`
  • 不支持赋值=

    1. `up1 = up; //error`

    2.3. 示例代码

    ```cpp

    include

    include

    using namespace std;

class A{ public: ~A(){ cout << “~A()” << endl; } A(){ cout << “A()” << endl; } void test(){ cout << “test()” << endl; } };

int main(){ A* p = new A; p->test(); //delete p;

  1. cout << "-----------" << endl;
  2. unique_ptr<A> uptr(new A);
  3. uptr->test();
  4. (*uptr).test();
  5. unique_ptr<A> uptr1;
  6. //uptr1 = uptr; // error
  7. return 0;

}

  1. ---
  2. <a name="NiWdL"></a>
  3. # 3. shared_ptr
  4. <a name="kwcRD"></a>
  5. ## 3.1. 概念
  6. - 共享拥有式智能指针,多个智能指针可以指向一个对象。
  7. <a name="lP1yj"></a>
  8. ## 3.2. 方法接口
  9. - 无参构造方法
  10. `shared_ptr<A> sptr;`
  11. - 单参构造方法
  12. `shared_ptr<A> sptr(new A);`
  13. - 拷贝构造
  14. `shared_pstr<A> sptr1(sptr);`
  15. - 赋值=
  16. `shared_ptr<A> sptr1; sptr1 = sptr;`
  17. - 使用计数:
  18. `.use_count() //返回所指对象的使用计数`
  19. <a name="W9QWe"></a>
  20. ## 3.3. shared_ptr的实现原理
  21. - 内部使用一个计数器,统计指向某个对象的智能指针个数。
  22. - 当计数器为0时,智能指针delete所指对象
  23. - 智能指针初始化时,计数器置1
  24. - 智能指针拷贝构造时,计数器加1
  25. - 智能指针赋值时,计数器加1
  26. - 智能指针析构时,计数器减1
  27. <a name="g6mHy"></a>
  28. ## 3.4. 示例代码
  29. ```cpp
  30. #include <iostream>
  31. #include <memory>
  32. using namespace std;
  33. class A{
  34. public:
  35. ~A(){
  36. cout << "~A()" << endl;
  37. }
  38. A(){
  39. cout << "A()" << endl;
  40. }
  41. void test(){
  42. cout << "test()" << endl;
  43. }
  44. };
  45. int main(){
  46. shared_ptr<A> sptr(new A);
  47. sptr->test();
  48. cout << "use_count : " << sptr.use_count() << endl;
  49. shared_ptr<A> sptr1(sptr);
  50. sptr1->test();
  51. cout << "use_count : " << sptr.use_count() << endl;
  52. shared_ptr<A> sptr2;
  53. sptr2 = sptr;
  54. sptr2->test();
  55. cout << "sptr.use_count : " << sptr.use_count() << endl;
  56. shared_ptr<A> sptr3(new A); // sptr3.use_count == 1
  57. cout << "sptr3.use_count : " << sptr3.use_count() << endl;
  58. return 0;
  59. }

4. weak_ptr

4.1. 概念(解决shared_ptr循环指向问题)

image.png
循环指向.bmp

  • weak_ptr称为弱指针,协助shared_ptr工作,解决shared_ptr的循环指向问题
  • weak_ptr总是指向一个用shared_ptr管理的对象,共享但不拥有对象

    4.2. 示例代码

    ```cpp

    include

    include

    using namespace std;

class B; // 前置声明

class A{ public: //shared_ptr msptrb; weak_ptr msptrb; // 采用上面屏蔽的做法,会导致循环指向问题 ~A(){ cout << “~A()” << endl; } A(){ cout << “A()” << endl; } void test(){ cout << “A : test()” << endl; } };

class B{ public: //shared_ptr msptra; weak_ptr msptra; // 采用上面屏蔽的做法,会导致循环指向问题 ~B(){ cout << “~B()” << endl; } B(){ cout << “B()” << endl; } void test(){ cout << “B : test()” << endl; } }; int main(){ shared_ptr sptra(new A); shared_ptr sptrb(new B);

  1. sptra->msptrb = sptrb;
  2. sptrb->msptra = sptra;
  3. cout << "sptra.use_count : " << sptra.use_count() << endl;
  4. cout << "sptrb.use_count : " << sptrb.use_count() << endl;
  5. return 0;

} ```