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í)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:詳解數(shù)據(jù)結(jié)構(gòu)C語言實現(xiàn)之循環(huán)隊列
本文標(biāo)題:C++11新特性之智能指針(shared_ptr/unique_ptr/weak_ptr)
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2130.html
您可能感興趣的文章
- 01-10HDOJ 1443 約瑟夫環(huán)的最新應(yīng)用分析詳解
 - 01-10基于typedef的用法詳解
 - 01-10VC中Tab control控件的用法詳細(xì)解析
 - 01-10C++實現(xiàn)數(shù)組的排序/插入重新排序/以及逆置操作詳解
 - 01-10通過c++11改進(jìn)我們的模式之改進(jìn)命令模式
 - 01-10c++11可變參數(shù)使用示例
 - 01-10c++11新增的便利算法實例分析
 - 01-10C++設(shè)置系統(tǒng)時間及系統(tǒng)時間網(wǎng)絡(luò)更新的方法
 - 01-10Vc++ 控件List Control用法總結(jié)
 - 01-10C語言實現(xiàn)在windows服務(wù)中新建進(jìn)程的方法
 


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
 - 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
 - 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
 - 04-02c語言用函數(shù)寫分段 用c語言表示分段
 - 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
 - 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
 - 04-02c語言沒有round函數(shù) round c語言
 - 04-02c語言分段函數(shù)怎么求 用c語言求分段
 - 04-02C語言中怎么打出三角函數(shù) c語言中怎
 - 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
 
隨機(jī)閱讀
- 01-10使用C語言求解撲克牌的順子及n個骰子
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 01-10C#中split用法實例總結(jié)
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10delphi制作wav文件的方法
 - 01-11ajax實現(xiàn)頁面的局部加載
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 


