智能指针
RAII(Resource Acquisition Is Initialization):
资源分配即初始化,定义封装一个类,用来实现调用构造函数时就可完成资源的分配和初始化,在调用析构函数就可完成资源的清理,以实现对资源的初始化和清理。
智能指针:
用自动化或者说智能的指针来实现对动态内存的释放。
它是一个类,有类似指针的功能。
常见的智能指针有:auto_ptr/scoped_ptr/scoped_array/shared_ptr/shared_array
一、AutoPtr
首先,先介绍AutoPtr,为防止一块空间释放两次浅拷贝导致的崩溃情况,我们的思想是权限转移,就是说你拷贝时要将你的两个指针指向同一块空间,可是这样会程序崩溃。解决如下:
1)老版AutoPtr
主要变量是_ptr,_owner,用bool型的_owner来控制权限转移,当它为false值时释放空间,保证释放一次。
[cpp] view plain copy
- #include
- using namespace std;
- template<class T>
- class AutoPtr
- {
- public:
- AutoPtr(T* ptr = NULL)
- :_ptr(ptr)
- , _owner(true)
- {}
- AutoPtr(const AutoPtr
& ap) - :_ptr(ap._ptr)
- {
- ap._owner = false;
- _owner = true;
- }
- AutoPtr
& operator=(const AutoPtr & ap) - {
- if (&s != this)
- {
- delete _ptr;
- _ptr = ap._ptr;
- ap._owner = false;
- _owner = true;
- }
- return *this;
- }
- ~AutoPtr()
- {
- if (_ptr)
- {
- delete _ptr;
- _ptr = NULL;
- _owner = false;
- }
- }
- T* operator->()
- {
- return _ptr;
- }
- T& operator*()
- {
- return *_ptr;
- }
- private:
- T* _ptr;
- bool _owner;
- };
- void Test()
- {
- AutoPtr<int> ap1(new int(1));
- AutoPtr<int> ap2(ap1);
- AutoPtr<int> ap3 = ap1;
- }
- int main()
- {
- Test();
- system(“pause”);
- return 0;
- }
缺陷:
[cpp] view plain copy
- if(……)
- {
- AutoPtr<int> ap2(ap1);
- ……
- }
出了作用域后ap2会释放空间还给系统,而ap2仍指向这块空间,会出现野指针。
2)新版AutoPtr
我们及时将之前的指针置成空,将这块空间的所有权交给现在的指针。
[cpp] view plain copy
- #include
- using namespace std;
- template<class T>
- class AutoPtr
- {
- public:
- AutoPtr(T* ptr)
- :_ptr(ptr)
- {}
- AutoPtr()
- :_ptr(NULL)
- {}
- AutoPtr
(AutoPtr & ap) //权限转移 - : _ptr(ap._ptr)
- {
- ap._ptr = NULL;
- }
- AutoPtr
& operator=(AutoPtr & ap) - {
- if (&ap != this)
- {
- delete _ptr;
- _ptr = ap._ptr;
- ap._ptr = NULL; //权限转移
- }
- return *this;
- }
- ~AutoPtr()
- {
- if (_ptr)
- {
- delete _ptr;
- _ptr = NULL;
- }
- }
- T& operator*()
- {
- return *_ptr;
- }
- private:
- T* _ptr;
- };
- void Test()
- {
- AutoPtr<int> ap1(new int(2));
- AutoPtr<int> ap2 = ap1;
- AutoPtr<int> ap3(new int(3));
- ap3 = ap1;
- }
- int main()
- {
- Test();
- system(“pause”);
- return 0;
- }
二、ScopedPtr
这是最实用的智能指针。
顾名思义,守卫的指针,思想就是防拷贝,在大多时候用不到拷贝构造和赋值运算符重载,那么我们做的就是写出构造函数和析构函数,拷贝构造和赋值运算符重载只声明不定义。这里有几点要说明:
(1)鉴于上面,我们写智能指针时,将拷贝构造和赋值运算符重载设置成保护或者私有的,这样就可以保证其他人在不知情的情况下(以为是我们忘记写定义了)无法写拷贝构造和赋值运算符重载的定义。
(2)既然不要定义,那为什么要声明呢,是不是可以不要,或许你们会这样想。不可以!原因是你不写,编译器会自动调用系统自身的拷贝构造和赋值运算符重载,这样就没办法做到防拷贝了。
下面,我们用ScopedPtr来实现简易版本的智能指针。
[cpp] view plain copy
- #include
- using namespace std;
- template<class T>
- class ScopedPtr
- {
- public:
- ScopedPtr(T* ptr)
- :_ptr(ptr)
- {}
- Scoped()
- :_ptr(NULL)
- {}
- ~ScopedPtr()
- {
- if (_ptr)
- {
- delete _ptr;
- _ptr = NULL;
- }
- }
- T& operator*()
- {
- return *_ptr;
- }
- T* GetPtr()
- {
- return _ptr;
- }
- protected:
- ScopedPtr
(const ScopedPtr & sp); - ScopedPtr
& operator = (const ScopedPtr & sp); - private:
- T* _ptr;
- };
- void Test()
- {
- ScopedPtr<int> sp1(new int(2));
- ScopedPtr<int> sp2 = sp1;
- ScopedPtr<int> sp3(new int(3));
- sp3 = sp1;
- }
- int main()
- {
- Test();
- system(“pause”);
- return 0;
- }
三、SharedPtr
共享指针,即思想就是引用计数,引入变量指针变量pCount,指向一块空间,对其计数,当只有一个指针指向空间时再释放资源,实现对其管理。初衷也是解决多个指针指向同一块空间释放多次会崩溃。这里不用static的整型的pCount在于,若有多个指针指向第一块空间,多个指针指向第二块空间,……,当改变一块空间的指向,该块空间的引用计数发生变化了,static的pCount会导致其他空间的引用计数也发生变化。
[cpp] view plain copy
- #include
- using namespace std;
- template<class T>
- class SharedPtr
- {
- public:
- SharedPtr(T* ptr)
- :_ptr(ptr)
- , _pCount(new long(1))
- {}
- SharedPtr()
- :_ptr(NULL)
- , _pCount(new long(1))
- {}
- SharedPtr
(const SharedPtr & sp) - : _ptr(sp._ptr)
- , _pCount(sp._pCount)
- {
- ++(*_pCount);
- }
- SharedPtr
& operator=(const SharedPtr & sp) - {
- if (&sp != this)
- {
- if (—(*_pCount) == 0)
- {
- delete _ptr;
- delete _pCount;
- }
- _ptr = sp._ptr;
- _pCount = sp._pCount;
- ++(*_pCount);
- }
- return *this;
- }
- ~SharedPtr()
- {
- if (_ptr)
- {
- if (—(*_pCount) == 0)
- {
- delete _ptr;
- delete _pCount;
- }
- }
- }
- T& operator*()
- {
- return *_ptr;
- }
- long GetCount()
- {
- return *(_pCount);
- }
- T* GetPtr()
- {
- return _ptr;
- }
- private:
- T* _ptr;
- long* _pCount;
- };
- void Test()
- {
- SharedPtr<int> sp1 = new int(1);
- SharedPtr<int> sp2 = sp1;
- SharedPtr<int> sp3 = new int(2);
- sp3 = sp1;
- }
- int main()
- {
- Test();
- system(“pause”);
- return 0;
- }
四、ScopedArray
ScopedArray与ScopedPtr区别在于:
ScopedArray管理数组,不可实现访问单个元素,而ScopedArray数组,可实现对数组元素的操控。
[cpp] view plain copy
- #include
- using namespace std;
- #include
- template<class T>
- class ScopedArray
- {
- public:
- ScopedArray(T* ptr = NULL)
- :_ptr(ptr)
- {}
- ~ScopedArray()
- {
- if (_ptr)
- {
- delete [] _ptr;
- _ptr = NULL;
- }
- }
- T& operator[](size_t index)
- {
- assert(index > 0);
- return _ptr[index];
- }
- protected:
- ScopedArray
(const ScopedArray & sp); - ScopedArray
& operator=(const ScopedArray & sp); - private:
- T* _ptr;
- };
- void Test()
- {
- ScopedArray<int> sp1(new int[10]);
- }
- int main()
- {
- Test();
- }
五、SharedPtr
同四。
[cpp] view plain copy
- #include
- using namespace std;
- #include
- template<class T>
- class SharedArray
- {
- public:
- SharedArray(T* ptr = NULL)
- :_ptr(ptr)
- , _pCount(new long(1))
- {}
- SharedArray
(const SharedArray & sp) - : _ptr(sp._ptr)
- {
- (*_pCount)++;
- }
- SharedArray
operator=(const SharedArray & sp) - {
- if (&s != this)
- {
- if (—(*pCount) == 0)
- {
- delete _ptr;
- _ptr = sp._ptr;
- (*pCount)++;
- }
- }
- return *this;
- }
- ~SharedArray()
- {
- if (_ptr)
- {
- if (—(*_pCount) == 0)
- {
- delete _ptr;
- delete _pCount;
- _ptr = NULL;
- _pCount = NULL;
- }
- }
- }
- T* operator[](size_t index)
- {
- assert(index);
- return _ptr[index];
- }
- private:
- T* _ptr;
- long *_pCount;
- };
- void Test()
- {
- SharedArray<int> sp1(new int[10]);
- SharedArray<int> sp2(sp1);
- SharedArray<int> sp3 = sp1;
- }
- int main()
- {
- Test();
- system(“pause”);
- return 0;
- }
还没有评论,来说两句吧...