C++ 智能指針的模擬實現(xiàn)實例
C++ 智能指針的模擬實現(xiàn)實例
1.引入
int main() { int *p = new int; //裸指針 delete p; return 0; }
在上面的代碼中定義了一個裸指針p,需要我們手動釋放。如果我們一不小心忘記釋放這個指針或者在釋放這個指針之前,發(fā)生一些異常,會造成嚴(yán)重的后果(內(nèi)存泄露)。而智能指針也致力于解決這種問題,使程序員專注于指針的使用而把內(nèi)存管理交給智能指針。
普通指針也容易出現(xiàn)指針懸掛問題,當(dāng)有多個指針指向同一個對象的時候,如果某一個指針delete了這個對象,所以這個指針不會對這個對象進(jìn)行操作,那么其他指向這個對象的指針呢?還在等待已經(jīng)被刪除的基礎(chǔ)對象并隨時準(zhǔn)備對它進(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,因為2早已經(jīng)被刪除了。
智能指針
智能指針是一個類,它把普通指針封裝起來,能實現(xiàn)和普通指針同樣的功能。不同的是智能指針能夠?qū)?nèi)存進(jìn)行自動管理,利用類對象出了作用域會調(diào)用析構(gòu)函數(shù),把對指針的釋放寫在析構(gòu)函數(shù)中,避免出現(xiàn)懸掛指針的情況。
智能指針(smart pointer)是存儲指向動態(tài)分配(堆)對象指針的類,用于生存期控制,能夠確保自動正確的銷毀動態(tài)分配的對象,防止內(nèi)存泄露。它的一種通用實現(xiàn)技術(shù)是使用引用計數(shù)(reference count)。智能指針類將一個計數(shù)器與類指向的對象相關(guān)聯(lián),引用計數(shù)跟蹤該類有多少個對象共享同一指針。每次創(chuàng)建類的新對象時,初始化指針并將引用計數(shù)置為1;當(dāng)對象作為另一對象的副本而創(chuàng)建時,拷貝構(gòu)造函數(shù)拷貝指針并增加與之相應(yīng)的引用計數(shù);對一個對象進(jìn)行賦值時,賦值操作符減少左操作數(shù)所指對象的引用計數(shù)(如果引用計數(shù)為減至0,則刪除對象),并增加右操作數(shù)所指對象的引用計數(shù);調(diào)用析構(gòu)函數(shù)時,構(gòu)造函數(shù)減少引用計數(shù)(如果引用計數(shù)減至0,則刪除基礎(chǔ)對象)。
智能指針就是模擬指針動作的類。所有的智能指針都會重載 -> 和 * 操作符。智能指針還有許多其他功能,比較有用的是自動銷毀。這主要是利用棧對象的有限作用域以及臨時對象(有限作用域?qū)崿F(xiàn))析構(gòu)函數(shù)釋放內(nèi)存。當(dāng)然,智能指針還不止這些,還包括復(fù)制時可以修改源對象等。智能指針根據(jù)需求不同,設(shè)計也不同(寫時復(fù)制,賦值即釋放對象擁有權(quán)限、引用計數(shù)等,控制權(quán)轉(zhuǎn)移等)。auto_ptr 即是一種常見的智能指針。
智能指針的實現(xiàn)(用類模板實現(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; }
模擬實現(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)志位 ,控制一個資源的訪問權(quán)限 }; int main() { CSmartptr<int> p1(new int); *p1 = 200; CSmartptr<Test> p2(new Test); p2->func(); return 0; }
帶有引用計數(shù)的智能指針(方便對資源的管理和釋放)
class CHeapTable { public: static CHeapTable& getInstance() { return mHeapTable; } //增加引用計數(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); } //減少引用計數(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); } } } //獲取引用計數(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)識堆內(nèi)存資源 int mcount; //標(biāo)識資源的引用計數(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();
以上就是智能指針的實例詳解,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持
相關(guān)文章
C++實現(xiàn)LeetCode(647.回文子字符串)
這篇文章主要介紹了C++實現(xiàn)LeetCode(647.回文子字符串),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Cocos2d-x學(xué)習(xí)入門之HelloWorld程序
這篇文章主要介紹了Cocos2d-x學(xué)習(xí)入門之HelloWorld程序,是學(xué)習(xí)Cocos2d-x的入門程序,其重要性不言而喻,需要的朋友可以參考下2014-08-08使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了使用C語言詳解霍夫曼樹數(shù)據(jù)結(jié)構(gòu),包括一道AMC相關(guān)的例題演示需要的朋友可以參考下2015-08-08C++中std::tuple和std::pair的實現(xiàn)
std::tuple和std::pair是兩種極具實用性的數(shù)據(jù)結(jié)構(gòu),本文主要介紹了C++中std::tuple和std::pair的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2025-02-02