智能指針的原理及其應(yīng)用

所謂智能指針就是自動(dòng)化管理指針?biāo)赶虻膭?dòng)態(tài)資源的釋放。

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了共和免費(fèi)建站歡迎大家使用!

那么智能指針的引用是為了解決哪些問(wèn)題呢?

代碼中經(jīng)常會(huì)忘掉釋放動(dòng)態(tài)開(kāi)辟的資源,引用智能指針可用于動(dòng)態(tài)資源管理,資源分配即初始化,定義一個(gè)類(lèi)來(lái)封裝資源的分配和釋放,在構(gòu)造函數(shù)中完成資源的分配和初始化,在析構(gòu)函數(shù)完成資源的清理,可以保證資源的正確初始化和釋放。

智能指針的原理:智能指針是一個(gè)類(lèi),這個(gè)類(lèi)的構(gòu)造函數(shù)中傳入一個(gè)普通指針,析構(gòu)函數(shù)中釋放傳入的指針。智能指針的類(lèi)是棧上的對(duì)象,智能指針指向堆上開(kāi)辟的空間,函數(shù)結(jié)束時(shí),棧上的函數(shù)會(huì)自動(dòng)被釋放,智能指針指向的內(nèi)存也會(huì)隨之消失,防止內(nèi)存泄漏。

智能指針的實(shí)現(xiàn)需要實(shí)現(xiàn)構(gòu)造、析構(gòu)、拷貝構(gòu)造、操作符重載。

幾種常見(jiàn)的智能指針的區(qū)別?

auto_ptr:就是內(nèi)部使用一個(gè)成員變量指向一塊內(nèi)存資源(構(gòu)造函數(shù)),并且在析構(gòu)函數(shù)中釋放內(nèi)存資源。auto_ptr的拷貝構(gòu)造和賦值操作符重載函數(shù)所接受的參數(shù)都是非const的引用類(lèi)型(即我們可以且修改資源),不能共享所有權(quán)(其它任意一個(gè)指針指向這塊內(nèi)存,它會(huì)把內(nèi)存給別的指針,自己不指向了)。權(quán)限轉(zhuǎn)移。

template <typename T>
class Auto_Ptr
{
public:
	Auto_Ptr(T *ptr = NULL)
		:_ptr(ptr)
	{}
	~Auto_Ptr()
	{
		if (_ptr != NULL)
		{
			delete _ptr;
			cout<<"~Auto_Ptr()"<<endl;
		}
	}
	Auto_Ptr(Auto_Ptr<T> &ap)
		:_ptr(ap._ptr)
	{
		ap._ptr = NULL;
	}
	Auto_Ptr<T> &operator= (Auto_Ptr<T> &ap)
	{
		if (this != &ap)
		{
			delete _ptr;
			_ptr = ap._ptr;
			ap._ptr = NULL;
		}
		return *this;
	}
private:
	T *_ptr;
};

void Test1()
{
	Auto_Ptr<int> a1(new int(1));
	Auto_Ptr<int> a2(a1);
	Auto_Ptr<int> a3(new int(2));
	a1 = a1;
	a2 = a3;
}

scoped_ptr有著與auto_ptr類(lèi)似的特性,scoped_ptr與auto_ptr最大的區(qū)別主要在于對(duì)內(nèi)存資源擁有權(quán)的處理。(auto_ptr在拷貝構(gòu)造時(shí)會(huì)從源auto_ptr自動(dòng)交出擁有權(quán),而scoped_ptr則不允許被拷貝)。scoped_ptr不能資源共享。

template <typename T>
class Scoped_Ptr
{
public:
	Scoped_Ptr(T *ptr)
		:_ptr(ptr)
	{}
	~Scoped_Ptr()
	{
		if (_ptr != NULL)
		{
			delete _ptr;
			cout<<"~Scoped_Ptr()"<<endl;
		}
	}

private:
	Scoped_Ptr(const Scoped_Ptr<T> &sp);
	Scoped_Ptr<T> &operator=(const Scoped_Ptr<T> &sp);
	T *_ptr;
};

void Test2()
{
	Scoped_Ptr<int> s1(new int(1));
	//Scoped_Ptr<int> s2(s1);
	Scoped_Ptr<int> s3(new int(2));
	//s1 = s1;
	//s2 = s3;
}

shared_ptr就是為了解決auto_ptr在對(duì)象所有權(quán)上的局限性,在使用引用計(jì)數(shù)的基礎(chǔ)上提供了可以共享所有權(quán)的智能指針。當(dāng)新增一個(gè)shared_ptr對(duì)該對(duì)象進(jìn)行管理時(shí),就將該對(duì)象的引用計(jì)數(shù)加一,同理減少一個(gè)時(shí),計(jì)數(shù)器減一。當(dāng)該對(duì)象的引用計(jì)數(shù)器為0時(shí),調(diào)用delete釋放其所占的內(nèi)存。

template <typename T>
class Shared_Ptr
{
public:
	Shared_Ptr(T *ptr)
		:_ptr(ptr)
		,_count(new int(1))
	{}
	~Shared_Ptr()
	{
		if(--(*_count) == 0)
		{
			delete _ptr;
			delete _count;
			cout<<"~Shared_Ptr()"<<endl;
		}
		(*_count)--;
	}
	Shared_Ptr(Shared_Ptr<T> &sp)
		:_ptr(sp._ptr)
		,_count(sp._count)
	{
		(*_count)++;
	}
	Shared_Ptr<T> &operator=(Shared_Ptr<T> &sp)
	{
		if (this != &sp)
		{
			if (--(*_count) == 0)
			{
				delete _ptr;
				delete _count;
			}
			_ptr = sp._ptr;
			_count = sp._count;
			(*_count)++;
		}
		return *this;
	}
private:
	T *_ptr;
	int *_count;
};

void Test3()
{
	Shared_Ptr<int> s1(new int(1));
	Shared_Ptr<int> s2(s1);
	Shared_Ptr<int> s3(new int(2));
	s1 = s1;
	s1 = s3;
}

shared_ptr存在循環(huán)引用的問(wèn)題,使用weak_ptr可以用來(lái)避免循環(huán)引用。但是weak_ptr對(duì)象引用資源時(shí)不會(huì)增加引用計(jì)數(shù),無(wú)法知道資源會(huì)不會(huì)被突然釋放,所以無(wú)法通過(guò)weak_ptr訪問(wèn)資源。在訪問(wèn)資源時(shí)weak_ptr必須先轉(zhuǎn)化為shared_ptr。

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using namespace std;
using namespace boost;
struct ListNode
{
	shared_ptr<ListNode > _prev;
	shared_ptr<ListNode > _next;
	//weak_ptr<ListNode > _prev;
	//weak_ptr<ListNode > _next;
	~ ListNode()
	{
		cout<<"~ListNode()" <<endl;
	}
};
void Test ()
{
// 循環(huán)引用問(wèn)題
	shared_ptr <ListNode > p1( new ListNode ());
	shared_ptr <ListNode > p2( new ListNode ());
	cout <<"p1->Count:" << p1. use_count()<<endl ;
	cout <<"p2->Count:" << p2. use_count()<<endl ;
	// p1節(jié)點(diǎn)的_next指向 p2節(jié)點(diǎn)
	p1->_next = p2;
	// p2節(jié)點(diǎn)的_prev指向 p1節(jié)點(diǎn)
	p2->_prev = p1;
	cout <<"p1->Count:" << p1. use_count ()<<endl ;
	cout <<"p2->Count:" << p2. use_count ()<<endl ;
}

新聞標(biāo)題:智能指針的原理及其應(yīng)用
網(wǎng)站URL:http://bm7419.com/article20/jcsijo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、商城網(wǎng)站、標(biāo)簽優(yōu)化定制開(kāi)發(fā)、企業(yè)建站、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)