C++智能指针及其简单实现
来源:互联网 发布:网络投资理财天天分红 编辑:程序博客网 时间:2024/05/16 06:06
使用智能指针的缘由
1. 考虑下边的简单代码:
1 int main()2 {3 int *ptr = new int(0);4 return 0;5 }
就如上边程序,我们有可能一不小心就忘了释放掉已不再使用的内存,从而导致资源泄漏(resoure leak,在这里也就是内存泄漏)。
2. 考虑另一简单代码:
1 int main()2 {3 int *ptr = new int(0);4 delete ptr;5 return 0;6 }
我们可能会心想,这下程序应该没问题了?可实际上程序还是有问题。上边程序虽然最后释放了申请的内存,但ptr会变成空悬指针(dangling pointer,也就是野指针)。空悬指针不同于空指针(nullptr),它会指向“垃圾”内存,给程序带去诸多隐患(如我们无法用if语句来判断野指针)。
上述程序在我们释放完内存后要将ptr置为空,即:
1 ptr = nullptr;
除了上边考虑到的两个问题,上边程序还存在另一问题:如果内存申请不成功,new会抛出异常,而我们却什么都没有做!所以对这程序我们还得继续改进(也可用try...catch...):
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int *ptr = new(nothrow) int(0); 7 if(!ptr) 8 { 9 cout << "new fails."10 return 0;11 }12 delete ptr;13 ptr = nullptr;14 return 0;15 }
3. 考虑最后一简单代码:
1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 int *ptr = new(nothrow) int(0); 7 if(!ptr) 8 { 9 cout << "new fails."10 return 0;11 }12 // 假定hasException函数原型是 bool hasException()13 if (hasException())14 throw exception();15 16 delete ptr;17 ptr = nullptr;18 return 0;19 }
当我们的程序运行到“if(hasException())”处且“hasException()”为真,那程序将会抛出一个异常,最终导致程序终止,而已申请的内存并没有释放掉。
当然,我们可以在“hasException()”为真时释放内存:
1 // 假定hasException函数原型是 bool hasException()2 if (hasException())3 {4 delete ptr;5 ptr = nullptr;6 throw exception();7 }
但,我们并不总会想到这么做。而且,这样子做也显得麻烦,不够人性化。
如果,我们使用智能指针,上边的问题我们都不用再考虑,因为它都已经帮我们考虑到了。
因此,我们使用智能指针的原因至少有以下三点:
1)智能指针能够帮助我们处理资源泄露问题;
2)它也能够帮我们处理空悬指针的问题;
3)它还能够帮我们处理比较隐晦的由异常造成的资源泄露。
智能指针
自C++11起,C++标准提供两大类型的智能指针:
1. Class shared_ptr实现共享式拥有(shared ownership)概念。多个智能指针可以指向相同对象,该对象和其相关资源会在“最后一个引用(reference)被销毁”时候释放。为了在结构复杂的情境中执行上述工作,标准库提供了weak_ptr、bad_weak_ptr和enable_shared_from_this等辅助类。
2. Class unique_ptr实现独占式拥有(exclusive ownership)或严格拥有(strict ownership)概念,保证同一时间内只有一个智能指针可以指向该对象。它对于避免资源泄露(resourece leak)——例如“以new创建对象后因为发生异常而忘记调用delete”——特别有用。
注:C++98中的Class auto_ptr在C++11中已不再建议使用。
shared_ptr
几乎每一个有分量的程序都需要“在相同时间的多处地点处理或使用对象”的能力。为此,我们必须在程序的多个地点指向(refer to)同一对象。虽然C++语言提供引用(reference)和指针(pointer),还是不够,因为我们往往必须确保当“指向对象”的最末一个引用被删除时该对象本身也被删除,毕竟对象被删除时析构函数可以要求某些操作,例如释放内存或归还资源等等。
所以我们需要“当对象再也不被使用时就被清理”的语义。Class shared_ptr提供了这样的共享式拥有语义。也就是说,多个shared_ptr可以共享(或说拥有)同一对象。对象的最末一个拥有者有责任销毁对象,并清理与该对象相关的所有资源。
shared_ptr的目标就是,在其所指向的对象不再被使用之后(而非之前),自动释放与对象相关的资源。
下边程序摘自《C++标准库(第二版)》5.2.1节:
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <memory> 5 using namespace std; 6 7 int main(void) 8 { 9 // two shared pointers representing two persons by their name10 shared_ptr<string> pNico(new string("nico"));11 shared_ptr<string> pJutta(new string("jutta"),12 // deleter (a lambda function) 13 [](string *p)14 { 15 cout << "delete " << *p << endl;16 delete p;17 }18 );19 20 // capitalize person names21 (*pNico)[0] = 'N';22 pJutta->replace(0, 1, "J");23 24 // put them multiple times in a container25 vector<shared_ptr<string>> whoMadeCoffee;26 whoMadeCoffee.push_back(pJutta);27 whoMadeCoffee.push_back(pJutta);28 whoMadeCoffee.push_back(pNico);29 whoMadeCoffee.push_back(pJutta);30 whoMadeCoffee.push_back(pNico);31 32 // print all elements33 for (auto ptr : whoMadeCoffee)34 cout << *ptr << " ";35 cout << endl;36 37 // overwrite a name again38 *pNico = "Nicolai";39 40 // print all elements41 for (auto ptr : whoMadeCoffee)42 cout << *ptr << " ";43 cout << endl;44 45 // print some internal data46 cout << "use_count: " << whoMadeCoffee[0].use_count() << endl;47 48 return 0;49 }
程序运行结果如下:
关于程序逻辑可见下图:
关于程序的几点说明:
1)对智能指针pNico的拷贝是浅拷贝,所以当我们改变对象“Nico”的值为“Nicolai”时,指向它的指针都会指向新值。
2)指向对象“Jutta”的有四个指针:pJutta和pJutta的三份被安插到容器内的拷贝,所以上述程序输出的use_count为4。
4)shared_ptr本身提供默认内存释放器(default deleter),调用的是delete,不过只对“由new建立起来的单一对象”起作用。当然我们也可以自己定义内存释放器,就如上述程序。不过值得注意的是,默认内存释放器并不能释放数组内存空间,而是要我们自己提供内存释放器,如:
1 shared_ptr<int> pJutta2(new int[10],2 // deleter (a lambda function) 3 [](int *p)4 { 5 delete[] p;6 }7 );
或者使用为unique_ptr而提供的辅助函数作为内存释放器,其内调用delete[]:
1 shared_ptr<int> p(new int[10], default_delete<int[]>());
unique_ptr
unique_ptr是C++标准库自C++11起开始提供的类型。它是一种在异常发生时可帮助避免资源泄露的智能指针。一般而言,这个智能指针实现了独占式拥有概念,意味着它可确保一个对象和其相应资源同一时间只被一个指针拥有。一旦拥有者被销毁或变成空,或开始拥有另一个对象,先前拥有的那个对象就会被销毁,其任何相应资源也会被释放。
现在,本文最开头的程序就可以写成这样啦:
1 #include <memory>2 using namespace std;3 4 int main()5 {6 unique_ptr<int> ptr(new int(0));7 return 0;8 }
智能指针简单实现
基于引用计数的智能指针可以简单实现如下(详细解释见程序中注释):
1 #include <iostream> 2 using namespace std; 3 4 template<class T> 5 class SmartPtr 6 { 7 public: 8 SmartPtr(T *p); 9 ~SmartPtr();10 SmartPtr(const SmartPtr<T> &orig); // 浅拷贝11 SmartPtr<T>& operator=(const SmartPtr<T> &rhs); // 浅拷贝12 private:13 T *ptr;14 // 将use_count声明成指针是为了方便对其的递增或递减操作15 int *use_count;16 };17 18 template<class T>19 SmartPtr<T>::SmartPtr(T *p) : ptr(p)20 {21 try22 {23 use_count = new int(1);24 }25 catch (...)26 {27 delete ptr;28 ptr = nullptr;29 use_count = nullptr;30 cout << "Allocate memory for use_count fails." << endl;31 exit(1);32 }33 34 cout << "Constructor is called!" << endl;35 }36 37 template<class T>38 SmartPtr<T>::~SmartPtr()39 {40 // 只在最后一个对象引用ptr时才释放内存41 if (--(*use_count) == 0)42 {43 delete ptr;44 delete use_count;45 ptr = nullptr;46 use_count = nullptr;47 cout << "Destructor is called!" << endl;48 }49 }50 51 template<class T>52 SmartPtr<T>::SmartPtr(const SmartPtr<T> &orig)53 {54 ptr = orig.ptr;55 use_count = orig.use_count;56 ++(*use_count);57 cout << "Copy constructor is called!" << endl;58 }59 60 // 重载等号函数不同于复制构造函数,即等号左边的对象可能已经指向某块内存。61 // 这样,我们就得先判断左边对象指向的内存已经被引用的次数。如果次数为1,62 // 表明我们可以释放这块内存;反之则不释放,由其他对象来释放。63 template<class T>64 SmartPtr<T>& SmartPtr<T>::operator=(const SmartPtr<T> &rhs)65 {66 // 《C++ primer》:“这个赋值操作符在减少左操作数的使用计数之前使rhs的使用计数加1,67 // 从而防止自身赋值”而导致的提早释放内存68 ++(*rhs.use_count);69 70 // 将左操作数对象的使用计数减1,若该对象的使用计数减至0,则删除该对象71 if (--(*use_count) == 0)72 {73 delete ptr;74 delete use_count;75 cout << "Left side object is deleted!" << endl;76 }77 78 ptr = rhs.ptr;79 use_count = rhs.use_count;80 81 cout << "Assignment operator overloaded is called!" << endl;82 return *this;83 }
测试程序如下:
1 #include <iostream> 2 #include "smartptr.h" 3 using namespace std; 4 5 int main() 6 { 7 // Test Constructor and Assignment Operator Overloaded 8 SmartPtr<int> p1(new int(0)); 9 p1 = p1;10 // Test Copy Constructor11 SmartPtr<int> p2(p1);12 // Test Assignment Operator Overloaded13 SmartPtr<int> p3(new int(1));14 p3 = p1;15 16 return 0;17 }
测试结果如下:
- C++智能指针及其简单实现
- C++智能指针及其简单实现
- C++智能指针及其简单实现
- C++智能指针及其简单实现
- 【C++】智能指针auto_ptr的简单实现
- c++智能指针及其实现
- 简单实现智能指针
- 智能指针简单实现
- C++-智能指针——简单实现分析
- c++:简单分析智能指针
- 智能指针的简单实现
- 智能指针的简单实现
- 智能指针的简单实现
- 智能指针的简单实现
- C++智能指针简单实现
- C++ 智能指针简单实现
- 动态内存与智能指针及其实现
- 【C++】智能指针的实现
- 静态编译与动态编译
- 数据结构上机实践第四周项目1
- SSM配置文件报错不允许有匹配 "[xX][mM][lL]" 的处理指令目标
- 批处理(.bat)无限循环,定时,固定时间间隔
- 深度学习在目标检测中的应用及其tensorflowAPI实践(三)
- C++智能指针及其简单实现
- Java实现邮箱发送
- 导出可执行的jar包,以及在项目中的使用
- 第四周【项目5- 循环双链表应用】 (2)
- CAD迷你看图最喜欢的操作方法
- MVC WebApi 文档生成注释说明的方法
- SSM框架集成(二)springmvc的集成
- Caffe实现多标签分类
- 深入理解Scala的隐式转换系统