C++11新特性之智能指針(shared_ptr/unique_ptr/weak_ptr)
shared_ptr基本用法
shared_ptr采用引用計數(shù)的方式管理所指向的對象。當(dāng)有一個新的shared_ptr指向同一個對象時(復(fù)制shared_ptr等),引用計數(shù)加1。當(dāng)shared_ptr離開作用域時,引用計數(shù)減1。當(dāng)引用計數(shù)為0時,釋放所管理的內(nèi)存。
這樣做的好處在于解放了程序員手動釋放內(nèi)存的壓力。之前,為了處理程序中的異常情況,往往需要將指針手動封裝到類中,通過析構(gòu)函數(shù)來釋放動態(tài)分配的內(nèi)存;現(xiàn)在這一過程就可以交給shared_ptr去做了。
一般我們使用make_shared來獲得shared_ptr。
cout<<"test shared_ptr base usage:"<<endl; shared_ptr<string> p1 = make_shared<string>(""); if(p1 && p1->empty()) *p1 = "hello"; auto p2 = make_shared<string>("world"); cout<<*p1<<' '<<*p2<<endl; cout<<"test shared_ptr use_count:"<<endl; cout<<"p1 cnt:"<<p1.use_count()<<"\tp2 cnt:"<<p2.use_count()<<endl; auto p3 = p2; cout<<"p1 cnt:"<<p1.use_count()<<"\tp2 cnt:"<<p2.use_count()<<"\tp3 cnt:"<<p3.use_count()<<endl; p2 = p1; cout<<"p1 cnt:"<<p1.use_count()<<"\tp2 cnt:"<<p2.use_count()<<"\tp3 cnt:"<<p3.use_count()<<endl;
shared_ptr和new
shared_ptr可以使用一個new表達(dá)式返回的指針進(jìn)行初始化。
cout<<"test shared_ptr and new:"<<endl; shared_ptr<int> p4(new int(1024)); //shared_ptr<int> p5 = new int(1024); // wrong, no implicit constructor cout<<*p4<<endl;
但是,不能將一個new表達(dá)式返回的指針賦值給shared_ptr。
另外,特別需要注意的是,不要混用new和shared_ptr!
void process(shared_ptr<int> ptr) { cout<<"in process use_count:"<<ptr.use_count()<<endl; } cout<<"don't mix shared_ptr and normal pointer:"<<endl; shared_ptr<int> p5(new int(1024)); process(p5); int v5 = *p5; cout<<"v5: "<<v5<<endl; int *p6 = new int(1024); process(shared_ptr<int>(p6)); int v6 = *p6; cout<<"v6: "<<v6<<endl;
上面的程序片段會輸出:
in process use_count:2
v5: 1024
in process use_count:1
v6: 0
可以看到,第二次process p6時,shared_ptr的引用計數(shù)為1,當(dāng)離開process的作用域時,會釋放對應(yīng)的內(nèi)存,此時p6成為了懸掛指針。
所以,一旦將一個new表達(dá)式返回的指針交由shared_ptr管理之后,就不要再通過普通指針訪問這塊內(nèi)存!
shared_ptr.reset
shared_ptr可以通過reset方法重置指向另一個對象,此時原對象的引用計數(shù)減一。
cout<<"test shared_ptr reset:"<<endl; cout<<"p1 cnt:"<<p1.use_count()<<"\tp2 cnt:"<<p2.use_count()<<"\tp3 nt:"<<p3.use_count()<<endl; p1.reset(new string("cpp11")); cout<<"p1 cnt:"<<p1.use_count()<<"\tp2 cnt:"<<p2.use_count()<<"\tp3 cnt:"<<p3.use_count()<<endl; shared_ptr deleter
可以定制一個deleter函數(shù),用于在shared_ptr釋放對象時調(diào)用。
void print_at_delete(int *p) { cout<<"deleting..."<<p<<'\t'<<*p<<endl; delete p; } cout<<"test shared_ptr deleter:"<<endl; int *p7 = new int(1024); shared_ptr<int> p8(p7, print_at_delete); p8 = make_shared<int>(1025);
unique_ptr基本用法
unique_ptr對于所指向的對象,正如其名字所示,是 獨占 的。所以,不可以對unique_ptr進(jìn)行拷貝、賦值等操作,但是可以通過release函數(shù)在unique_ptr之間轉(zhuǎn)移控制權(quán)。
cout<<"test unique_ptr base usage:"<<endl; unique_ptr<int> up1(new int(1024)); cout<<"up1: "<<*up1<<endl; unique_ptr<int> up2(up1.release()); cout<<"up2: "<<*up2<<endl; //unique_ptr<int> up3(up1); // wrong, unique_ptr can not copy //up2 = up1; // wrong, unique_ptr can not copy unique_ptr<int> up4(new int(1025)); up4.reset(up2.release()); cout<<"up4: "<<*up4<<endl;
unique_ptr作為參數(shù)和返回值
上述對于拷貝的限制,有兩個特殊情況,即unique_ptr可以作為函數(shù)的返回值和參數(shù)使用,這時雖然也有隱含的拷貝存在,但是并非不可行的。
unique_ptr<int> clone(int p) { return unique_ptr<int>(new int(p)); } void process_unique_ptr(unique_ptr<int> up) { cout<<"process unique ptr: "<<*up<<endl; } cout<<"test unique_ptr parameter and return value:"<<endl; auto up5 = clone(1024); cout<<"up5: "<<*up5<<endl; process_unique_ptr(move(up5)); //cout<<"up5 after process: "<<*up5<<endl; // would cause segmentfault
這里的std::move函數(shù),以后再單獨具體細(xì)說^_^
unique_ptr deleter
unique_ptr同樣可以設(shè)置deleter,和shared_ptr不同的是,它需要在模板參數(shù)中指定deleter的類型。好在我們有decltype這個利器,不然寫起來好麻煩。
cout<<"test unique_ptr deleter:"<<endl; int *p9 = new int(1024); unique_ptr<int, decltype(print_at_delete) *> up6(p9, print_at_delete); unique_ptr<int> up7(new int(1025)); up6.reset(up7.release());
weak_ptr
weak_ptr一般和shared_ptr配合使用。它可以指向shared_ptr所指向的對象,但是卻不增加對象的引用計數(shù)。這樣就有可能出現(xiàn)weak_ptr所指向的對象實際上已經(jīng)被釋放了的情況。因此,weak_ptr有一個lock函數(shù),嘗試取回一個指向?qū)ο蟮膕hared_ptr。
cout<<"test weak_ptr basic usage:"<<endl; auto p10 = make_shared<int>(1024); weak_ptr<int> wp1(p10); cout<<"p10 use_count: "<<p10.use_count()<<endl; //p10.reset(new int(1025)); // this will cause wp1.lock() return a false obj shared_ptr<int> p11 = wp1.lock(); if(p11) cout<<"wp1: "<<*p11<<" use count: "<<p11.use_count()<<endl;
總結(jié)
shared_ptr采用引用計數(shù)的方式管理所指向的對象。
shared_ptr可以使用一個new表達(dá)式返回的指針進(jìn)行初始化;但是,不能將一個new表達(dá)式返回的指針賦值給shared_ptr。
一旦將一個new表達(dá)式返回的指針交由shared_ptr管理之后,就不要再通過普通指針訪問這塊內(nèi)存。
shared_ptr可以通過reset方法重置指向另一個對象,此時原對象的引用計數(shù)減一。
可以定制一個deleter函數(shù),用于在shared_ptr釋放對象時調(diào)用。
unique_ptr對于所指向的對象,是獨占的。
不可以對unique_ptr進(jìn)行拷貝、賦值等操作,但是可以通過release函數(shù)在unique_ptr之間轉(zhuǎn)移控制權(quán)。
unique_ptr可以作為函數(shù)的返回值和參數(shù)使用。
unique_ptr同樣可以設(shè)置deleter,需要在模板參數(shù)中指定deleter的類型。
weak_ptr一般和shared_ptr配合使用。它可以指向shared_ptr所指向的對象,但是卻不增加對象的引用計數(shù)。
weak_ptr有一個lock函數(shù),嘗試取回一個指向?qū)ο蟮膕hared_ptr。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++如何實現(xiàn)BitMap數(shù)據(jù)結(jié)構(gòu)
這篇文章主要介紹了C++如何實現(xiàn)BitMap數(shù)據(jù)結(jié)構(gòu),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07C語言鏈表實現(xiàn)通訊錄系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細(xì)介紹了C語言鏈表實現(xiàn)通訊錄系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-06-06C語言中關(guān)于sizeof 和 strlen的區(qū)別分析
本文通過示例簡單分析了4種情況下C語言中sizeof 和 strlen的區(qū)別,算是個人經(jīng)驗的一個小小的總結(jié),如有遺漏還請大家告知。2015-02-02