C++智能指针之01

C++智能指针

在C++中通过new/delete进行内存分配和释放,但是实际编码过程中可能由于程序员的疏忽导致内存泄漏问题的出现。 在Java中GC(垃圾回收机制)进行内存管理。C++11同样增加了内存管理功能,这就是智能指针。

  • 智能指针本质上是一个模板类,一般使用的这个类的对象,而不是指针
  • 智能指针体现在内存释放问题上,用智能指针管理new的对象,将不在需要手动delete

实现原理

智能指针是存储指向动态分配(堆区)对象指针的类,内部通过引用计数实现对管理对象内存的监控,每使用一次引用计数+1,每析构一次引用计数减1,当减为0,释放所指向管理对象的内存。

通过RAII机制,用于生存期的控制,当离开智能智能的作用域会自动销毁动态分配的对象,防止发生内存泄漏问题。

智能指针的分类

C++11提供了三种智能指针

  • std::shared_ptr 共享的智能指针
  • std::unique_ptr 独占的智能指针
  • std::weak_ptr 弱引用的智能指针,它不共享指针,不能操作资源,是用来shared_ptr的

下面详细介绍下智能指针

  • shared_ptr
    get()函数:返回数据的指针的引用
    use_count() : 返回管理对象的智能指针个数
    swap() : 交换两个智能指针管理的对象
    reset() : 重置智能指针对象
    针对部分场景需要自己手动添加删除器去释放对象内存

  • weak_ptr
    弱引用指针,不会累计计数
    weak_ptr只能通过shared_ptr或者weak_ptr来构造
    主要应用场景:为了解决shared_ptr循环引用内存导致无法释放问题
    不能使用*访问,可以使用->取值
    通过成员函数lock()获取shared_ptr对象然后在访问数据

  • unique_ptr
    禁止拷贝和赋值 独占型
    任何时候unique_ptr操作管理对象,永远只有一个有效
    可以通过move()函数转交所有权
    reset函数结合release函数移交所有权

以下对于三种类型分别举例:

  • shared_ptr
#include <iostream>
#include <string>
#include <memory>
using namespace std;

class MyPerson {
public:
	MyPerson(int age) :age(age) 
	{ 
		cout << "MyPerson构造函数被调用!" << endl; 
	}
	
	MyPerson()
	{
		cout << "MyPerson无参构造函数被调用" << endl;
	}
	
	~MyPerson() 
	{ 
		cout << "MyPerson析构函数被调用!" << endl; 
	}

	void print()
	{
		cout << "MyPerson的年龄:" << age << endl;
	}
private:
	int age;
};

//这里智能指针类对象作为入参,会发送拷贝构造所以use_count智能指针数会+1
//如果传入引用就不会到导致use_count增加
void printPersonData(shared_ptr<MyPerson> pMyPerson)
{
	pMyPerson->print();
	cout << "管理MyPerson对象的指针数:" << pMyPerson.use_count() << endl;
}

int main()
{
	//针对基本数据类型
	shared_ptr<int> pInt(new int(111));
	//访问数据
	cout << *pInt << endl;
	cout << "使用get()获取数据:" << *pInt.get() << endl;
	//use_count返回对象的指针数
	cout << "管理对象的指针数:" << pInt.use_count() << endl;

	shared_ptr<int> pInt2(pInt); //使用拷贝构造函数
	cout << "管理对象的指针数:" << pInt.use_count() << endl;
	cout << "管理对象的指针数:" << pInt2.use_count() << endl;

	shared_ptr<int> pInt3;
	if (!pInt3)
	{
		cout << "空智能指针对象" << endl;
	}
	pInt3 = pInt2;
	cout << "管理对象的指针数:" << pInt3.use_count() << endl;

	//使用swap交换两个智能指针对象
	shared_ptr<int> aa(new int(11));
	shared_ptr<int> bb(new int(22));
	aa.swap(bb);
	cout << "交换后" << endl;
	cout << "aa=" << *aa << "\tbb=" << *bb << endl;

	bb.reset(new int(123));  //重置
	cout << "*bb=" << *bb << endl;

	//针对自定义数据类型
	//初始化
	shared_ptr<MyPerson> pMyPerson(new MyPerson(10));
    //访问数据
	pMyPerson->print();
	cout << "管理MyPerson对象的指针数:" << pMyPerson.use_count() << endl;
	printPersonData(pMyPerson);
	//shared_ptr智能指针管理的对象会自动释放内存
	system("pause");
	return 0;
}

结果分析:
1、使用shared_ptr智能指针管理自定义对象可以使用->访问对象的成员函数(shared_ptr智能指针类内部实现了重定义operator->)
2、使用shared_ptr智能指针管理的对象会自动释放内存,无需手动释放(针对于new创建内存)
3、使用use_count()返回管理对象的智能指针个数

111
使用get()获取数据:111
管理对象的指针数:1
管理对象的指针数:2
管理对象的指针数:2
空智能指针对象
管理对象的指针数:3
交换后
aa=22   bb=11
*bb=123
MyPerson构造函数被调用!
MyPerson的年龄:10
管理MyPerson对象的指针数:1
MyPerson的年龄:10
管理MyPerson对象的指针数:2

针对如下场景

不支持创建连续内存
shared_ptr<MyPerson> pMyPerson2(new MyPerson(10)[4]);

如果想要实现上面常见则自己需要在创建智能指针对象类时添加删除器(自己手动写释放内存过程)`

//带删除器写法; 自己写释放内存过程
	shared_ptr<MyPerson> pMyPerson2(new MyPerson[4], [](MyPerson* array) {delete[] array; });

  • weak_ptr
#include <iostream>
#include <string>
#include <memory>
using namespace std;

class MyPerson {
public:
	MyPerson(int age) :age(age) 
	{ 
		cout << "MyPerson构造函数被调用!" << endl; 
	}
	
	MyPerson()
	{
		cout << "MyPerson无参构造函数被调用" << endl;
	}
	
	~MyPerson() 
	{ 
		cout << "MyPerson析构函数被调用!" << endl; 
	}

	void print()
	{
		cout << "MyPerson的年龄:" << age << endl;
	}
private:
	int age;
};
 
int main()
{
	//针对自定义数据类型
	//初始化
	shared_ptr<MyPerson> pMyPerson(new MyPerson(10));
    //访问数据
	pMyPerson->print();
	cout << "管理MyPerson对象的指针数:" << pMyPerson.use_count() << endl;

	//weak_ptr初始化只能通过shared_ptr或者weak_ptr
	weak_ptr< MyPerson> pWeakPtr(pMyPerson);
	//通过weak_ptr不会导致引用计数增加
	cout << "管理MyPerson对象的指针数:" << pMyPerson.use_count() << endl;
	//*pWeakPtr.print(); weak_ptr不存在这种访问方式

	//通过lock()获取shared_ptr对象
	pWeakPtr.lock().get()->print();

	system("pause");
	return 0;
}
  • unique_ptr
#include <iostream>
#include <string>
#include <memory>
using namespace std;

class MyPerson {
public:
	MyPerson(int age) :age(age) 
	{ 
		cout << "MyPerson构造函数被调用!" << endl; 
	}
	
	MyPerson()
	{
		cout << "MyPerson无参构造函数被调用" << endl;
	}
	
	~MyPerson() 
	{ 
		cout << "MyPerson析构函数被调用!" << endl; 
	}

	void print()
	{
		cout << "MyPerson的年龄:" << age << endl;
	}
private:
	int age;
};

int main()
{
	//针对自定义数据类型
	//初始化
	unique_ptr<MyPerson> pMyPerson(new MyPerson(111));
	
	//unique_ptr不能拷贝和赋值 如下:
	//unique_ptr<MyPerson> pMyPerson2(pMyPerson);
	unique_ptr<MyPerson> pMyPerson3;
	//pMyPerson3 = pMyPerson;

	//move移交所有权后原来的则不能继续使用
	pMyPerson3 = move(pMyPerson);
	pMyPerson3->print();


	//reset重置对象
	pMyPerson.reset(new MyPerson(20));

	unique_ptr<MyPerson> pMyPerson4;
	pMyPerson4.reset(pMyPerson3.release());
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值