Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2161220
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: C/C++

2011-05-09 15:35:51

智能指针的作用是防止new出来(处于堆上)的对象因指针操作失误没有及时delete而导致内存泄露。

智能指针原理是用一个计数类RefCount存储要指向的地址和指向该地址的指针数,根据指针数来决定是否应该delete申请的内存。

如图:

C++之简单的智能指针模型

SmartPtr ptr1=new CMyObject("first object");
当将一个new出来的内存地址赋值给SmartPtr指针ptr1时,会用一个RefCount类m_Ptr存储这个内存地址,并将m_iCount设为1。

 SmartPtr ptr2=ptr1;

当其他SmartPtr指针ptr2指向这个指针的时候,并不是单单将new出的目标地址赋给ptr2指针,而是将整个ptr1的RefCount(目标地址和指针计数器)传到ptr2,然后m_iCount会加1,表示多了一个指针指向这个地址。 

ptr1=new CMyObject("second object");
当ptr1将指向新地址的时候,会将之前RefCount的m_iCount减1,表示少了一个指针指向之前地址,如果旧地址的计数器m_iCount为0,则表示已经没有指针指向这个地址了,将会自动调用delete释放旧的地址。

如图:当多个Smart指针指向一个地址的时候,各指针共享一个RefCount类对象,m_iCount记录指针数。

C++之简单的智能指针模型

简单的实例代码如下:

 

  1. #include <iostream>
  2. using namespace std;
  3. template<class T>class RefCount;

  4. template <class T> class SmartPtr
  5. {
  6. private:
  7.  RefCount<T> *m_RefCount;
  8. public:
  9.  SmartPtr(T* Ptr)
  10.  {
  11.   m_RefCount=new RefCount<T>(Ptr);
  12.   m_RefCount->InitCount();
  13.  }
  14.  SmartPtr(RefCount<T>* Ptr)
  15.  {
  16.   Ptr->AddCount();
  17.   m_RefCount=Ptr;
  18.  }
  19.  ~SmartPtr()
  20.  {
  21.   m_RefCount->Destroy();
  22.  }
  23.  RefCount<T>* GetPtr()
  24.  {
  25.   return m_RefCount;
  26.  }

  27.  SmartPtr<T>& operator = (T* Ptr)
  28.  {
  29.   if (this->m_RefCount!=NULL)
  30.   {
  31.    this->m_RefCount->Destroy();
  32.   }
  33.   m_RefCount=new RefCount<T>(Ptr);
  34.   m_RefCount->InitCount();
  35.   return *this;

  36.  }
  37.  SmartPtr<T>& operator = (SmartPtr<T>& Ptr)
  38.  {
  39.   if (this->m_RefCount!=NULL)
  40.   {
  41.    this->m_RefCount->Destroy();
  42.   }
  43.   Ptr.m_RefCount->AddCount();
  44.   this->m_RefCount=Ptr.m_RefCount;
  45.   return Ptr;
  46.  }
  47.   T& operator * (){return *(m_RefCount->m_Ptr);};
  48. };


  49. template<class T>class RefCount
  50. {
  51.  friend class SmartPtr<T>;
  52. private:
  53.  T* m_Ptr;
  54.  int m_iCount;
  55. public:
  56.  void AddCount()
  57.  {
  58.   m_iCount++;
  59.  }
  60.  void InitCount()
  61.  {
  62.   m_iCount=1;
  63.  }
  64.  void Destroy()
  65.  {
  66.   m_iCount--;
  67.   if (m_iCount==0)
  68.   {
  69.    delete m_Ptr;
  70.   }
  71.  }
  72.  RefCount(T* ptr)
  73.  {
  74.   m_Ptr=ptr;
  75.  }
  76.  ~RefCount()
  77.  {
  78.  }
  79.  T* GetPtr()
  80.  {
  81.   return m_Ptr;
  82.  }
  83. };
  84. class CMyObject
  85. {
  86.  char *name;
  87. public:
  88.  CMyObject(char* aname){name=aname;printf("create %s\n",name);}
  89.  ~CMyObject(){printf("delete %s\n",name);}
  90. };

  91. void main()
  92. {
  93.  SmartPtr<CMyObject> ptr1(new CMyObject("1"));
  94.  SmartPtr<CMyObject> ptr2=new CMyObject("2");
  95.  ptr1=ptr2;
  96.  ptr1=new CMyObject("3");
  97.  ptr2=SmartPtr<CMyObject>(new CMyObject("4"));
  98.  ptr2=NULL;
  99. }
转自:http://blog.sina.com.cn/s/blog_62ea3be40100r97j.html
阅读(919) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~