C++ 智能指針的模擬實(shí)現(xiàn)實(shí)例
C++ 智能指針的模擬實(shí)現(xiàn)實(shí)例
1.引入
int main()
{
int *p = new int; //裸指針
delete p;
return 0;
}
在上面的代碼中定義了一個(gè)裸指針p,需要我們手動(dòng)釋放。如果我們一不小心忘記釋放這個(gè)指針或者在釋放這個(gè)指針之前,發(fā)生一些異常,會(huì)造成嚴(yán)重的后果(內(nèi)存泄露)。而智能指針也致力于解決這種問(wèn)題,使程序員專注于指針的使用而把內(nèi)存管理交給智能指針。
普通指針也容易出現(xiàn)指針懸掛問(wèn)題,當(dāng)有多個(gè)指針指向同一個(gè)對(duì)象的時(shí)候,如果某一個(gè)指針delete了這個(gè)對(duì)象,所以這個(gè)指針不會(huì)對(duì)這個(gè)對(duì)象進(jìn)行操作,那么其他指向這個(gè)對(duì)象的指針呢?還在等待已經(jīng)被刪除的基礎(chǔ)對(duì)象并隨時(shí)準(zhǔn)備對(duì)它進(jìn)行操作。于是懸垂指針就形成了,程序崩潰也“指日可待”。
int main()
{
int *p1 = new int(2);
int *p2 = p1;
int *p3 = p2;
cout<<*p1<<endl;
cout<<*p2<<endl;
cout<<*p3<<endl;
delete p1;
cout<<*p2<<endl;
return 0;
}
輸出結(jié)果
2 2 2 -572662307
輸出的結(jié)果*p2的結(jié)果并不是期待中2,因?yàn)?早已經(jīng)被刪除了。


智能指針
智能指針是一個(gè)類,它把普通指針?lè)庋b起來(lái),能實(shí)現(xiàn)和普通指針同樣的功能。不同的是智能指針能夠?qū)?nèi)存進(jìn)行自動(dòng)管理,利用類對(duì)象出了作用域會(huì)調(diào)用析構(gòu)函數(shù),把對(duì)指針的釋放寫在析構(gòu)函數(shù)中,避免出現(xiàn)懸掛指針的情況。
智能指針(smart pointer)是存儲(chǔ)指向動(dòng)態(tài)分配(堆)對(duì)象指針的類,用于生存期控制,能夠確保自動(dòng)正確的銷毀動(dòng)態(tài)分配的對(duì)象,防止內(nèi)存泄露。它的一種通用實(shí)現(xiàn)技術(shù)是使用引用計(jì)數(shù)(reference count)。智能指針類將一個(gè)計(jì)數(shù)器與類指向的對(duì)象相關(guān)聯(lián),引用計(jì)數(shù)跟蹤該類有多少個(gè)對(duì)象共享同一指針。每次創(chuàng)建類的新對(duì)象時(shí),初始化指針并將引用計(jì)數(shù)置為1;當(dāng)對(duì)象作為另一對(duì)象的副本而創(chuàng)建時(shí),拷貝構(gòu)造函數(shù)拷貝指針并增加與之相應(yīng)的引用計(jì)數(shù);對(duì)一個(gè)對(duì)象進(jìn)行賦值時(shí),賦值操作符減少左操作數(shù)所指對(duì)象的引用計(jì)數(shù)(如果引用計(jì)數(shù)為減至0,則刪除對(duì)象),并增加右操作數(shù)所指對(duì)象的引用計(jì)數(shù);調(diào)用析構(gòu)函數(shù)時(shí),構(gòu)造函數(shù)減少引用計(jì)數(shù)(如果引用計(jì)數(shù)減至0,則刪除基礎(chǔ)對(duì)象)。
智能指針就是模擬指針動(dòng)作的類。所有的智能指針都會(huì)重載 -> 和 * 操作符。智能指針還有許多其他功能,比較有用的是自動(dòng)銷毀。這主要是利用棧對(duì)象的有限作用域以及臨時(shí)對(duì)象(有限作用域?qū)崿F(xiàn))析構(gòu)函數(shù)釋放內(nèi)存。當(dāng)然,智能指針還不止這些,還包括復(fù)制時(shí)可以修改源對(duì)象等。智能指針根據(jù)需求不同,設(shè)計(jì)也不同(寫時(shí)復(fù)制,賦值即釋放對(duì)象擁有權(quán)限、引用計(jì)數(shù)等,控制權(quán)轉(zhuǎn)移等)。auto_ptr 即是一種常見(jiàn)的智能指針。
智能指針的實(shí)現(xiàn)(用類模板實(shí)現(xiàn))
class Test
{
public:
Test()
{
cout<<"Test()"<<endl;
}
~Test()
{
cout<<"~Test()"<<endl;
}
void func()
{
cout<<"call Test::func()"<<endl;
}
};
template<typename T>
class CSmartptr
{
public:
CSmartptr(T *ptr):_ptr(ptr)
{cout<<"CSmartptr()"<<endl;}
CSmartptr(const CSmartptr<T> &other)
{
_ptr = new T;
*ptr = *other._ptr;
}
~CSmartptr()
{
cout<<"~CSmartptr()"<<endl;
delete _ptr;
}
void relase() const
{
((CSmartptr<T> *)this)->owns = false;
}
T& operator*()
{
return *_ptr;
}
const T& operator*()const {return *_ptr;}
T *operator->()
{
return _ptr;
}
const T *operator->()const {return _ptr;}
private:
T *_ptr;
};
int main()
{
CSmartptr<int> p1(new int);
*p1 = 200;
CSmartptr<Test> p2(new Test);
p2->func();
return 0;
}
模擬實(shí)現(xiàn)auto_ptr
template<typename T>
class CSmartptr
{
public:
CSmartptr(T *ptr):_ptr(ptr),owns(true){cout<<"CSmartptr()"<<endl;}
CSmartptr(const CSmartptr<T> &other)
{
other.relase();
_ptr = other._ptr;
}
~CSmartptr()
{
cout<<"~CSmartptr()"<<endl;
if( owns == true)
{
cout<<"~CSmartptr()"<<endl;
delete _ptr;
}
}
void relase() const
{
((CSmartptr<T> *)this)->owns = false;
}
T& operator*()
{
return *_ptr;
}
const T& operator*()const {return *_ptr;}
T *operator->()
{
return _ptr;
}
const T *operator->()const {return _ptr;}
private:
T *_ptr;
bool owns; //標(biāo)志位 ,控制一個(gè)資源的訪問(wèn)權(quán)限
};
int main()
{
CSmartptr<int> p1(new int);
*p1 = 200;
CSmartptr<Test> p2(new Test);
p2->func();
return 0;
}
帶有引用計(jì)數(shù)的智能指針(方便對(duì)資源的管理和釋放)
class CHeapTable
{
public:
static CHeapTable& getInstance()
{
return mHeapTable;
}
//增加引用計(jì)數(shù)
void addRef(void *ptr)
{
pthread_mutex_lock(mutex);
list<Node>::iterator it = find(mList.begin(),
mList.end(), ptr); // Node == Node it->mpaddr
if(it == mList.end())
{
mList.push_front(ptr);
cout<<"new addr:"<<ptr<<" ref:"<<1<<endl;
}
else
{
it->mcount++;
cout<<"add addr:"<<ptr<<" ref:"<<it->mcount<<endl;
}
pthread_mutex_unlock(mutex);
}
//減少引用計(jì)數(shù)的
void delRef(void *ptr)
{
list<Node>::iterator it = find(mList.begin(),
mList.end(), ptr);
if(it != mList.end())
{
it->mcount--;
cout<<"del addr:"<<ptr<<" ref:"<<it->mcount<<endl;
if(it->mcount == 0)
{
mList.erase(it);
}
}
}
//獲取引用計(jì)數(shù)的
int getRef(void *ptr)
{
list<Node>::iterator it = find(mList.begin(),
mList.end(), ptr);
if(it != mList.end())
{
return it->mcount;
}
return 0;
}
private:
CHeapTable(){}
static CHeapTable mHeapTable;
struct Node
{
Node(void *ptr=NULL):mpaddr(ptr),mcount(1){}
bool operator==(const Node &src)
{
return mpaddr == src.mpaddr;
}
void *mpaddr; //標(biāo)識(shí)堆內(nèi)存資源
int mcount; //標(biāo)識(shí)資源的引用計(jì)數(shù)
};
list<Node> mList;
};
CHeapTable CHeapTable::mHeapTable;
template<typename T>
class CSmartPtr
{
public:
CSmartPtr(T *ptr = NULL)
:mptr(ptr)
{
if(mptr != NULL)
{
addRef();
}
}
~CSmartPtr()
{
delRef();
if(0 == getRef())
{
delete mptr;
mptr = NULL;
}
}
CSmartPtr(const CSmartPtr<T> &src)
:mptr(src.mptr)
{
if(mptr != NULL)
{
addRef();
}
}
CSmartPtr<T>& operator=(const CSmartPtr<T> &src)
{
if(this == &src)
return *this;
delRef();
if(0 == getRef())
{
delete mptr;
mptr = NULL;
}
mptr = src.mptr;
if(mptr != NULL)
{
addRef();
}
}
T& operator*(){return *mptr;}
const T& operator*()const{return *mptr;}
T* operator->(){return mptr;}
const T* operator->()const{return mptr;}
void addRef(){mHeapTable.addRef(mptr);}
void delRef(){mHeapTable.delRef(mptr);}
int getRef(){return mHeapTable.getRef(mptr);}
private:
T *mptr;
static CHeapTable &mHeapTable;
};
template<typename T>
CHeapTable& CSmartPtr<T>::mHeapTable = CHeapTable::getInstance();
以上就是智能指針的實(shí)例詳解,如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(647.回文子字符串)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(647.回文子字符串),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
Cocos2d-x學(xué)習(xí)入門之HelloWorld程序
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)入門之HelloWorld程序,是學(xué)習(xí)Cocos2d-x的入門程序,其重要性不言而喻,需要的朋友可以參考下2014-08-08
使用C語(yǔ)言詳解霍夫曼樹(shù)數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了使用C語(yǔ)言詳解霍夫曼樹(shù)數(shù)據(jù)結(jié)構(gòu),包括一道AMC相關(guān)的例題演示需要的朋友可以參考下2015-08-08
C++中std::tuple和std::pair的實(shí)現(xiàn)
std::tuple和std::pair是兩種極具實(shí)用性的數(shù)據(jù)結(jié)構(gòu),本文主要介紹了C++中std::tuple和std::pair的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02
C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)簡(jiǎn)易版
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)圖書(shū)管理系統(tǒng)簡(jiǎn)易版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03

