shared_ptr基本用法
shared_ptr采用引用計(jì)數(shù)的方式管理所指向的對(duì)象。當(dāng)有一個(gè)新的shared_ptr指向同一個(gè)對(duì)象時(shí)(復(fù)制shared_ptr等),引用計(jì)數(shù)加1。當(dāng)shared_ptr離開(kāi)作用域時(shí),引用計(jì)數(shù)減1。當(dāng)引用計(jì)數(shù)為0時(shí),釋放所管理的內(nèi)存。
這樣做的好處在于解放了程序員手動(dòng)釋放內(nèi)存的壓力。之前,為了處理程序中的異常情況,往往需要將指針手動(dòng)封裝到類中,通過(guò)析構(gòu)函數(shù)來(lái)釋放動(dòng)態(tài)分配的內(nèi)存;現(xiàn)在這一過(guò)程就可以交給shared_ptr去做了。
一般我們使用make_shared來(lái)獲得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可以使用一個(gè)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 constructorcout<<*p4<<endl;
但是,不能將一個(gè)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;
上面的程序片段會(huì)輸出:
in process use_count:2
v5: 1024
in process use_count:1
v6: 0
可以看到,第二次process p6時(shí),shared_ptr的引用計(jì)數(shù)為1,當(dāng)離開(kāi)process的作用域時(shí),會(huì)釋放對(duì)應(yīng)的內(nèi)存,此時(shí)p6成為了懸掛指針。
所以,一旦將一個(gè)new表達(dá)式返回的指針交由shared_ptr管理之后,就不要再通過(guò)普通指針訪問(wèn)這塊內(nèi)存!
shared_ptr.reset
shared_ptr可以通過(guò)reset方法重置指向另一個(gè)對(duì)象,此時(shí)原對(duì)象的引用計(jì)數(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
可以定制一個(gè)deleter函數(shù),用于在shared_ptr釋放對(duì)象時(shí)調(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對(duì)于所指向的對(duì)象,正如其名字所示,是 獨(dú)占 的。所以,不可以對(duì)unique_ptr進(jìn)行拷貝、賦值等操作,但是可以通過(guò)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 copyunique_ptr<int> up4(new int(1025));up4.reset(up2.release());cout<<"up4: "<<*up4<<endl;
unique_ptr作為參數(shù)和返回值
上述對(duì)于拷貝的限制,有兩個(gè)特殊情況,即unique_ptr可以作為函數(shù)的返回值和參數(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ù),以后再單獨(dú)具體細(xì)說(shuō)^_^
unique_ptr deleter
unique_ptr同樣可以設(shè)置deleter,和shared_ptr不同的是,它需要在模板參數(shù)中指定deleter的類型。好在我們有decltype這個(gè)利器,不然寫起來(lái)好麻煩。
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所指向的對(duì)象,但是卻不增加對(duì)象的引用計(jì)數(shù)。這樣就有可能出現(xiàn)weak_ptr所指向的對(duì)象實(shí)際上已經(jīng)被釋放了的情況。因此,weak_ptr有一個(gè)lock函數(shù),嘗試取回一個(gè)指向?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 objshared_ptr<int> p11 = wp1.lock();if(p11) cout<<"wp1: "<<*p11<<" use count: "<<p11.use_count()<<endl;
總結(jié)
shared_ptr采用引用計(jì)數(shù)的方式管理所指向的對(duì)象。
shared_ptr可以使用一個(gè)new表達(dá)式返回的指針進(jìn)行初始化;但是,不能將一個(gè)new表達(dá)式返回的指針賦值給shared_ptr。
一旦將一個(gè)new表達(dá)式返回的指針交由shared_ptr管理之后,就不要再通過(guò)普通指針訪問(wèn)這塊內(nèi)存。
shared_ptr可以通過(guò)reset方法重置指向另一個(gè)對(duì)象,此時(shí)原對(duì)象的引用計(jì)數(shù)減一。
可以定制一個(gè)deleter函數(shù),用于在shared_ptr釋放對(duì)象時(shí)調(diào)用。
unique_ptr對(duì)于所指向的對(duì)象,是獨(dú)占的。
不可以對(duì)unique_ptr進(jìn)行拷貝、賦值等操作,但是可以通過(guò)release函數(shù)在unique_ptr之間轉(zhuǎn)移控制權(quán)。
unique_ptr可以作為函數(shù)的返回值和參數(shù)使用。
unique_ptr同樣可以設(shè)置deleter,需要在模板參數(shù)中指定deleter的類型。
weak_ptr一般和shared_ptr配合使用。它可以指向shared_ptr所指向的對(duì)象,但是卻不增加對(duì)象的引用計(jì)數(shù)。
weak_ptr有一個(gè)lock函數(shù),嘗試取回一個(gè)指向?qū)ο蟮膕hared_ptr。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答