智能指针,RALL
c++中,程序员自己申请的空间需要自己去释放,如果忘记,可能会导致资源的泄露
//c++
auto ptr = new vecotr<int>();
//java
//使用了垃圾回收技术,不需要人为管理,相关的虚拟机会自动释放不需要使用的资源
ArrayList<int> list = new ArrayList<int>();
C++解决方法:RALL
c++特有的资源管理方式:
- 利用c++中一个对象出了作用域会被自动析构,因此我们只需要在构造函的时候申请空间,在析构函数的时候释放空间
智能指针的分类
unique_ptr
shared_ptr
weak_ptr
unique_ptr
强指针
(替换auto_ptr)unique_ptr实现独占式拥有或严格拥有概念,保证同一时间内只有一个智能指针可以指向该对象。它对于避免资源泄露(例如“以new创建对象后因为发生异常而忘记调用delete”)特别有用。
unique_ptr<string> pu2;
pu2 = pu1; // #1 不允许 pu2会存在一段时间
unique_ptr<string> pu3;
pu3 = unique_ptr<string>(new string ("You")); // #2 允许 后面的临时存在
unique_ptr<string> ps1, ps2;
ps1 = demo("hello");
ps2 = move(ps1); //提供函数move用于转移,将自己置空
ps1 = demo("alexia");
cout << *ps2 << *ps1 << endl;
reset
release //=不会释放原来的内部指针,而是将自己置空
shared_ptr
弱指针
可以多个智能指针指向一个
成员函数:
use_count 返回引用计数的个数
unique 返回是否是独占所有权( use_count 为 1)
swap 交换两个 shared_ptr 对象(即交换所拥有的对象)
reset 放弃内部对象的所有权或拥有对象的变更, 会引起原有对象的引用计数的减少
get 返回内部对象(指针), 由于已经重载了()方法, 因此和直接使用对象是一样的.如
shared_ptr<int> sp(new int(1));
问题
循环引用:
两个shared_ptr指向对方,无法正常调用析构,导致内存泄漏
weak_ptr
share_ptr虽然已经很好用了,但是有一点share_ptr智能指针还是有内存泄露的情况,当两个对象相互使用一个shared_ptr成员变量指向对方,会造成循环引用,使引用计数失效,从而导致内存泄漏。
weak_ptr 是一种不控制对象生命周期的智能指针, 它指向一个 shared_ptr 管理的对象. 进行该对象的内存管理的是那个强引用的shared_ptr, weak_ptr只是提供了对管理对象的一个访问手段。weak_ptr 设计的目的是为配合 shared_ptr 而引入的一种智能指针来协助 shared_ptr 工作, 它只可以从一个 shared_ptr 或另一个 weak_ptr 对象构造, 它的构造和析构不会引起引用记数的增加或减少。weak_ptr是用来解决shared_ptr相互引用时的死锁问题,如果说两个shared_ptr相互引用,那么这两个指针的引用计数永远不可能下降为0,资源永远不会释放。它是对对象的一种弱引用,不会增加对象的引用计数,和shared_ptr之间可以相互转化,shared_ptr可以直接赋值给它,它可以通过调用lock函数来获得shared_ptr。
shared_ptr<int> sptr(new int(3));
weak_ptr<int> wptr = sptr;
if(!wptr.expired)
shared_ptr<int> sptr2 = wptr.lock();
手撕指针代码
#include<iostream>
#include<cstdio>
using namespace std;
template<typename T>
class SmartPointr{
public:
SmartPointer(T *ptr){
ref = ptr;
ref_count = (unsigned int*)malloc(sizeof(unsigned));
++*ref_rount;
}
SmartPointer(const SmartPointer& other){
ref = other.ptr;
ref_count = other.ref_count;
++*ref_rount;
}
SmartPointer& operator=(const SmartPointer& other){
if(this == other)
return *this;
if(--*ref_count==0)
clear();
ref=ptr;
ref_count=other.ref_count;
++*ref_count;
}
T& operator*(){
return *ref;
}
T* operator->(){
return ref;
}
~SmartPointer(){
if(--*ref_count==0)
clear();
}
private:
clear(){
delete ref;
free(ref_count);
ref = NULL;
ref_count=NULL;
}
protected:
T* ref;
unsinged int* ref_count;
}