C++进阶 —— 智能指针

目录

一,内存泄露

二,智能指针

智能指针的原理

三,C++库中的智能指针

auto_ptr

unique _ptr

shared_ptr

weak_ptr

附:RAII扩展        


一,内存泄露

内存泄露,是指因为疏忽或错误造成程序未能释放那些已不在使用的内存的情况;内存泄露并不是内存在物理上的消失,而是应用程序分配某段内存后,因为设计错误失去了对该段内存的控制,因而造成内存的浪费;

内存泄露的危害,长期运行的程序出现内存泄露,影响很大,如操作系统、后台服务器等,出现内存泄露会导致响应越来越慢,最终卡死;

内存泄露的分类

  • 堆内存泄露Heap Leak,指程序执行中通过malloc/calloc/realloc/new等从堆中分配的一块内存,用完后必须通过相应的free或delete释放掉;如这部分内存没有释放,就会造成这部分空间无法再被使用;
  • 系统资源泄露,指程序使用系统分配的资源,如套接字、文件描述符、管道等没有使用对应的函数释放掉,导致系统资源的浪费,严重可导致系统效能不稳定、系统执行不稳定;

避免内存泄露方式

  • 前期良好的设计规范,编码规范,申请的内存空间记着匹配释放;如碰上异常时,就算注意释放,也有可能出问题,需要智能指针来管理才有保证;采用RAII思想或智能指针来管理资源;
  • 公司内部规范,使用内部实现的私有内部管理库,这套库自带内存泄露检测的功能选项;出问题了使用内存泄露工具检测;
  • 内存泄露非常常见,有事前预防型(如智能指针等),事后查错型(如检测工具);

二,智能指针

        RAII(Resource Acquisition Is Initialization),是一种利用对象生命周期来控制程序资源的申请和释放;在对象构造时获取资源,控制对资源的访问使之在对象生命周期内始终保持有效,最后在对象析构的时候释放资源;借此,实际上把管理资源的责任托管给了一个对象;

        智能指针,是使用RAII技术的一种封装指针的类对象,重载了相应的运算符,可像指针一样运算,但会自动回收堆上的内存空间,从而避免内存泄露;

好处:

  • 不需要显式地释放资源;
  • 对象所需资源在其生命周期内始终有效;
//RAII技术
template<class T>
class SmartPtr
{
public:
	SmartPtr(T* ptr = nullptr)
		:_ptr(ptr)
	{}
	~SmartPtr()
	{
		if (_ptr)
			delete _ptr;
	}
private:
	T* _ptr;
};

void Func(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	SmartPtr<int> sp(tmp);
}

int main()
{
	try
	{
		int arr[] = { 1,2,3,4,5 };
		Func(arr, 5);
	}
	catch (const exception& e)
	{
		cout << e.what() << endl;
	}
	return 0;
}

智能指针的原理

        上述SmartPtr还不能将其称为智能指针,因为其不具有指针行为;指针还可解引用及访问所指空间,因此AutoPtr模板类中还需有*、->重载,才可以让其像指针一样去使用;

//智能指针类似指针,当还可自动回收内存
template<class T>
class SmartPtr
{
public:
	SmartPtr(T* ptr = nullptr)
		:_ptr(ptr)
	{}
	~SmartPtr()
	{
		if (_ptr)
			delete _ptr;
	}
	T& operator*() { return *_ptr; }
	T* operator->() { return _ptr; }
private:
	T* _ptr;
};

struct Date
{
	int _year;
	int _month;
	int _day;
};

int main()
{
	SmartPtr<int> sp1(new int);
	*sp1 = 10;
	cout << *sp1 << endl;

	SmartPtr<Date> sp2(new Date);
	(*sp2)._year = 2020; // sp2.operator*()._year;
	sp2->_year = 2021; // sp2.operator->()->_year;
	return 0;
}

三,C++库中的智能指针

        C++库中的智能指针都定义在头文件<memory>中;

auto_ptr

  • 此模板为指针提供了一个有限的垃圾回功能,在auto_ptr对象销毁时自动回收指向的内存;
  • C++98,当拷贝或赋值auto_ptr对象时,被拷贝或赋值对象就销毁释放了;
  • C++11,此模板已废弃,使用具有类似功能的新模板unique_ptr(提升了安全性);
#include<iostream>
#include<memory>
using namespace std;

int main()
{
	auto_ptr<int> ap1(new int);
	*ap1 = 10;
	auto_ptr<int> ap2(ap1); //拷贝构造后,ap1即销毁
	auto_ptr<int> ap3(new int);
	ap3 = ap2; //赋值后,ap2即销毁
	return 0;
}

  •  实现原理,管理权移交思想;
template<class T>
class AutoPtr
{
public:
	AutoPtr(T* ptr = NULL)
		:_ptr(ptr)
	{}
	~AutoPtr()
	{
		if (_ptr)
			delete _ptr;
	}
	AutoPtr(AutoPtr<T>& ap)
		:_ptr(ap._ptr)
	{
		ap._ptr = NULL;
	}
	AutoPtr<T>& operator=(AutoPtr<T>& ap)
	{
		if (this != &ap)
		{
			if (_ptr)
				delete _ptr;
			_ptr = ap._ptr;
			ap._ptr = NULL;
		}
		return *this;
	}
private:
	T* _ptr;
};

unique _ptr

  • 此模板为指针提供了一个有限的垃圾回收功能,与内置的指针相比几乎没有额外的开销;
  • 自身销毁时、operator=或显式调用reset使其值发生更改时,都会使用deleter自动删除其管理的对象;
  • 此模板类有两个成员:pointer(可使用get/release访问)、deleter(可使用get_deleter访问);
  • unique_ptr对象复制了有限的指针功能,通过*、->、[ ]管理对象;为安全起见,不支持指针算法,只支持移动赋值(禁止拷贝赋值);
//为安全起见,不支持拷贝和赋值
unique_ptr (const unique_ptr&) = delete;	
unique_ptr& operator= (const unique_ptr&) = delete;
//支持移动构造
unique_ptr (unique_ptr&& x) noexcept;
unique_ptr& operator= (unique_ptr&& x) noexcept;
int main()
{
	unique_ptr<int> up1(new int), up2(new int);
	*up1 = 10, *up2 = 20;

	up1 = move(up2);
	up1.reset(new int);

	auto up3 = unique_ptr<int>(up1.get());
	up3.release();

	up1.get_deleter()(up1.get());
	return 0;
}
  • 实现原理,简单粗暴防拷贝;
template<class T>
class UniquePtr
{
public:
	UniquePtr(T* ptr = nullptr)
		:_ptr(ptr)
	{}
	~UniquePtr()
	{
		if (_ptr)
			delete _ptr;
	}
	T& operator*() { return *_ptr };
	T& operator->() { return _ptr };
private:
	//C++98防拷贝方式,只私有声明不实现
	UniquePtr(UniquePtr<T> const&);
	UniquePtr& operator=(UniquePtr<T> const&);

	//C++11防拷贝方式,delete
	UniquePtr(UniquePtr<T> const&) = delete;
	UniquePtr& operator=(UniquePtr<T> const&) = delete;
private:
	T* _ptr;
};

shared_ptr

  • 此模板为指针提供了一个有限的垃圾回收功能,可与其他对象共享管理;
  • 此类对象可获取和共享指针的所有权,最后一个所有者负责释放;
  • 自身销毁时、operator=或显式调用reset使其值发生更改时,就会释放共享对象的所有权;当所有对象都释放了所有权,就会使用deleter删除其管理的对象;
  • 此类对象只能通过拷贝shared_ptr对象来共享所有权,如通过同一指针(非共享指针)构建的对象,则不会共享,会造成一个对象释放时,另一个对象无处释放;
  • shared_ptr对象关联两个指针:stored pointerowned pointer;通常它们关联相同对象,但alias构造时,会不同;
  • 不拥有所有权的指针为empty shared_ptr,不指向任何对象的指针为null shared_ptr;empty shared_ptr不一定是null shared_ptr,null shared_ptr也不一定是empty shared_ptr;
  • shared_ptr对象复制了有限的指针功能,通过*、->访问所指对象;为安全起见,不支持指针算法;
auto ptr = new int;
//共享同一块空间,共享所有权
shared_ptr<int> ptr1(ptr);
shared_ptr<int> ptr2(ptr1);
//共享同一块空间,但不共享所有权
shared_ptr<int> ptr1(ptr);
shared_ptr<int> ptr2(ptr);

实现原理,通过引用计数的方式来实现多个shared_ptr对象之间共享资源;

  • shared_ptr在其内部,给每个资源都维护着一份计数,用来记录该份资源被几个对象共享;
  • 在对象被销毁时(析构),说明该资源不使用了,引用计数减1;
  • 如引用计数为0,说明是最后一个使用该资源的对象,必须释放该资源;
  • 如引用计数不为0,说明还有对象在使用该资源,不可释放,否则成野指针;
template<class T>
class SharedPtr
{
public:
	SharedPtr(T* ptr=nullptr)
		:_ptr(ptr)
		,_pRefCount(new int(1))
		,_pMutex(new mutex)
	{}
	~SharedPtr()
	{
		Release();
	}
	SharedPtr(const SharedPtr<T>& sp)
		:_ptr(sp._ptr)
		, _pRefCount(sp._pRefCount)
		, _pMutex(sp._pMutex)
	{
		AddRefCount();
	}
	SharedPtr<T>& operator=(const SharedPtr<T>& sp)
	{
		if (_ptr != sp._ptr)
		{
			Release();
			_ptr = sp._ptr;
			_pRefCount(sp._pRefCount);
			_pMutex(sp._pMutex);
			AddRefCount();
		}
		return *this;
	}
	T& operator*() { return *_ptr; }
	T* operator->() { return _ptr; }
	int UseCount() { return *_pRefCount; }
	T* Get() { return _ptr };

	void AddRefCount()
	{
		_pMutex->lock();
		++(*_pRefCount);
		_pMutex->unlock();
	}
private:
	void Release()
	{
		bool deleteflag = false;
		_pMutex.lock();
		if (--(*_pRefCount == 0))
		{
			delete _ptr;
			delete _pRefCount;
			deleteflag = true;
		}
		if (deleteflag == true)
			delete _pMutex;
	}
private:
	T* _ptr;
	int* _pRefCount;
	mutex* _pMutex; // 互斥锁
};

shared_ptr线程安全问题

  • 智能指针对象中引用计数是多个智能指针对象共享的,两个线程中智能指针的引用计数同时++或--,引用计数原来是1,++两次可能还是2,这样引用计数就错乱了,会导致资源未释放或程序崩溃;所以智能指针中引用计数++或--是需要加锁的,也就是说引用计数的操作是线程安全的;
  • 智能指针管理的对象存放在堆上,两个线程同时去访问,会导致线程安全问题;

shared_ptr循环引用

struct listnode
{
	~listnode() { cout << "~listnode()" << endl; }
	int _data;
	shared_ptr<listnode> _prev;
	shared_ptr<listnode> _next;
};

int main()
{
	shared_ptr<listnode> node1(new listnode);
	shared_ptr<listnode> node2(new listnode);
	cout << node1.use_count() << endl;
	cout << node2.use_count() << endl;

	node1->_next = node2;
	node2->_prev = node1;
	cout << node1.use_count() << endl;
	cout << node2.use_count() << endl;
	return 0;
}
  • 两个智能指针对象node1、node2,指向两个节点,引用计数变成1,不需手动delete;
  • node1->_next指向node2,node2->_next指向node1,引用计数变成2;
  • node1/node2析构,引用计数减到1,但是_next还指向下一个节点,_prev还指向上个节点;也就是说_next析构了,node2就释放,_prev析构了,node1就释放;
  • 但是_next属于node的成员,node2释放了,_next才会析构,而node1由_prev管理,_prev属于node2成员,所以这就叫循环引用,谁也不会释放;
  • 针对上述问题,可使用weak_ptr;

weak_ptr

  • 一种弱引用的智能指针类型,可用于解决shared_ptr循环引用的问题;
  • 使用weak_ptr,不会增加引用计数;
struct listnode
{
	~listnode() { cout << "~listnode()" << endl; }
	int _data;
	weak_ptr<listnode> _prev;
	weak_ptr<listnode> _next;
};

int main()
{
	shared_ptr<listnode> node1(new listnode);
	shared_ptr<listnode> node2(new listnode);
	cout << node1.use_count() << endl;
	cout << node2.use_count() << endl;

	node1->_next = node2;
	node2->_prev = node1;
	cout << node1.use_count() << endl;
	cout << node2.use_count() << endl;
	return 0;
}

如不是new出来的对象,使用删除器;

//仿函数删除器
template<class T>
struct FreeFunc
{
	void operator()(T* ptr)
	{
		cout << "free:" << ptr << endl;
		free(ptr);
	}
};

template<class T>
struct DeleteArrayFunc
{
	void operator()(T* ptr)
	{
		cout << "delete[]:" << ptr << endl;
		delete[] ptr;
	}
};

int main()
{
	FreeFunc<int> freefunc;
	shared_ptr<int> sp1((int*)malloc(4), freefunc);

	DeleteArrayFunc<int> deletearrayfunc;
	shared_ptr<int>sp2((int*)malloc(4), deletearrayfunc);
}

C++11和boost中智能指针的关系

  • C++98中产生了第一个智能指针auto_ptr;
  • C++boost给出了更加实用的scoped_ptr、shared_ptr、weak_ptr;
  • C++TR1,引入了shared_ptr等,但TR1不是标准版;
  • C++11,引入了unique_ptr、shared_ptr、weak_ptr,uniqu_ptr对应boost的scoped_ptr,且这些智能指针的实现原理参考了boost中的实现;

附:RAII扩展        

        RAII思想除了可以原来设计智能指针,还可以用来设计守卫锁,防止异常安全导致的死锁问题;

#include<iostream>
#include<thread>
#include<mutex>
using namespace std;

template<class Mutex>
class LockGuard
{
public:
	LockGuard(Mutex& mtx)
		:_mutex(mtx)
	{
		_mutex.lock();
	}
	~LockGuard()
	{
		_mutex.unlock();
	}
	LockGuard(const LockGuard<Mutex>&) = delete;
private:
	Mutex& _mutex; //必须使用引用
};

mutex mtx;
static int n = 0;

void Func()
{
	for (size_t i = 0; i < 1000000; ++i)
	{
		LockGuard<mutex> lock(mtx);
		++n;
	}
}

int main()
{
	int begin = clock();
	thread t1(Func);
	thread t2(Func);

	t1.join();
	t2.join();

	int end = clock();
	cout << n << endl;
	cout << "cost time:" << end - begin << endl;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值