C++中智能指针代码的示例分析

发布时间:2022-03-04 15:09:28 作者:小新
来源:亿速云 阅读:95

这篇文章主要为大家展示了“C++中智能指针代码的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“C++中智能指针代码的示例分析”这篇文章吧。

前言

如果在程序中使用new从堆分配内存,等到不再需要时,应使用delete将其释放,C++引入了智能指针auto_ptr,以帮助自动完成这个过程,但是aoto_ptr也有其局限性,因此从Boost库中又引入了三种智能指针unique_ptr shared_ptr weak_ptr。

1,aoto_ptr

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <memory>
#include <string>
#include <iostream>
#include <ostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	auto_ptr<string>  ptr1(new string("this is ptr!"));
	auto_ptr<string>  ptr2;
	ptr2 = ptr1;
	cout << &ptr2<<endl;
	cout << *ptr2 << endl;
	return 0;
}

003AFBC0
this is ptr!

但是如果输出的是ptr1,程序会如何呢?

#include "stdafx.h"
#include <memory>
#include <string>
#include <iostream>
#include <ostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	auto_ptr<string>  ptr1(new string("this is ptr!"));
	auto_ptr<string>  ptr2;
	ptr2 = ptr1;
	cout << &ptr1 <<endl;
	cout << *ptr1 << endl;  #这一步程序会崩溃
	return 0;
}

崩溃原因: 首先ptr2 = ptr1表示ptr1将访问的权限给了ptr2,同时意味了ptr1已经没有访问字符串的权限,因此会报错。

那如何解决这个问题呢?引入了unique_ptr

2,unique_ptr

#include "stdafx.h"
#include <memory>
#include <string>
#include <iostream>
#include <ostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	unique_ptr<string> ptr1(new string("this is unique_ptr"));
	unique_ptr<string> ptr2;
	ptr2 = ptr1;  #这一步编译器会报错
	return 0;
}

unique_ptr 替代auto_ptr实现独占式,可以理解成,同一时刻只能有一个unique_ptr指向给定对象,unique_ptr对象始终是关联的原始指针的唯一所有者。无法复制unique_ptr对象,它只能移动。(这样可以保证,不会出现auto_ptr那样运行时会出现的隐藏内存崩溃问题)

int _tmain(int argc, _TCHAR* argv[])
{
	unique_ptr<string> ptr1(new string("this is unique_ptr"));
	unique_ptr<string> ptr2;
	cout << &ptr1 << endl;
	unique_ptr<string> ptr3(new string("other unique_ptr"));
	cout << &ptr3 << endl;
	cout << *ptr3 << endl;
	return 0;
}

00D9F8B4
00D9F89C
other unique_ptr

3,share_ptr

// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <memory>
#include <string>
#include <iostream>
#include <ostream>
using namespace std;
class base{
public:
	base()
	{
		cout << "begin..." << endl;
	};
	~base()
	{
		cout << "end..." << endl;
	}
};
int _tmain(int argc, _TCHAR* argv[])
{
	base *a = new base();
	shared_ptr<base> ptr1(a);
	//shared_ptr<base> ptr2(a);    ## 如果加上这句程序会崩溃,双重管理陷阱,a对象被删除了两次
	return 0;
}

begin...

end...

#include "stdafx.h"
#include <memory>
#include <string>
#include <iostream>
#include <ostream>
using namespace std;
class CB;
class CA
{
public:
	CA()
	{
		cout << "CA call ..."<< endl;
	}
	~CA()
	{
		cout << "~CA call..."<< endl;
	}
	void setPtr(shared_ptr<CB> &ptr)
	{ 
		m_ptr_b = ptr;
	}
	int getCount()
	{
		return m_ptr_b.use_count();
	}
private:
	shared_ptr<CB> m_ptr_b;
};
class CB
{
public:
	CB()
	{
		cout << "CB call..." << endl;
	}
	~CB()
	{
		cout << "~CB call..." << endl;
	}
	void setPtr(shared_ptr<CA> ptr)
	{
		m_ptr_a = ptr;
	}
	int getCount()
	{
		return m_ptr_a.use_count();
	}

private:
	shared_ptr<CA> m_ptr_a;
};
int _tmain(int argc, _TCHAR* argv[])
{
	shared_ptr<CA> ptr_a(new CA);
	shared_ptr<CB> ptr_b(new CB);
	cout << " CA count is : " << ptr_a->getCount()<<endl;
	cout << "CB count is:" << ptr_b->getCount()<< endl;
	ptr_a->setPtr(ptr_b);
	ptr_b->setPtr(ptr_a);
	cout << " CA count is : " << ptr_a->getCount() << endl;
	cout << "CB count is:" << ptr_b->getCount() << endl;
	return 0;
}

上面这段程序的思路用下面张图可以清晰的表示

图片和代码主要参考的是这篇很棒的博文:智能指针(三):weak_ptr浅析

C++中智能指针代码的示例分析

C++中智能指针代码的示例分析

运行结果后发现并没有调用析构函数释放内存,以后存在内存泄漏的风险

那如何去解决这个问题呢?,可以通过引入weak_ptr来解决,但是weak_ptr需要与share_ptr配合使用

4, weak_ptr

通过在两个类中的一个成员变量改为weak_ptr对象,因为weak_ptr不会增加引用计数,使得引用形不成环,最后就可以正常的释放内部的对象,不会造成内存泄漏

class CB
{
public:
	CB()
	{
		cout << "CB call..." << endl;
	}
	~CB()
	{
		cout << "~CB call..." << endl;
	}
	void setPtr(shared_ptr<CA> ptr)
	{
		m_ptr_a = ptr;
	}
	int getCount()
	{
		return m_ptr_a.use_count();
	}

private:
	///shared_ptr<CA> m_ptr_a;
	weak_ptr<CA> m_ptr_a;  ## 改为weak_ptr对象
};

C++中智能指针代码的示例分析

C++中智能指针代码的示例分析

以上是“C++中智能指针代码的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. C++智能指针shared_ptr分析
  2. C++代码功能块的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++

上一篇:div如何使用

下一篇:如何解决import包时报Java程序包不存在的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》