C++11 新特性之智慧指標
這是C++11新特性介紹的第五部分,涉及到智慧指標的相關內容(shared_ptr, unique_ptr, weak_ptr)。當然筆者以前也寫過關於c++智慧指標部分的知識總結,這次的話是講到c++11的特性,再次總結一下。
不想看toy code的讀者可以直接拉到文章最後看這部分的總結。
shared_ptr
shared_ptr 基本用法
shared_ptr採用引用計數的方式管理所指向的物件。當有一個新的shared_ptr指向同一個物件時(複製shared_ptr等),引用計數加1。當shared_ptr離開作用域時,引用計數減1。當引用計數為0時,釋放所管理的記憶體。
這樣做的好處在於解放了程式設計師手動釋放記憶體的壓力。之前,為了處理程式中的異常情況,往往需要將指標手動封裝到類中,通過解構函式來釋放動態分配的記憶體;現在這一過程就可以交給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表示式返回的指標進行初始化。
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表示式返回的指標賦值給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的引用計數為1,當離開process的作用域時,會釋放對應的記憶體,此時p6成為了懸掛指標。
所以,一旦將一個new表示式返回的指標交由shared_ptr管理之後,就不要再通過普通指標訪問這塊記憶體!
shared_ptr.reset
shared_ptr可以通過reset方法重置指向另一個物件,此時原物件的引用計數減一。
cout<<"test shared_ptr reset:"<shared_ptr deleter
可以定製一個deleter函式,用於在shared_ptr釋放物件時呼叫。
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對於所指向的物件,正如其名字所示,是獨佔的。所以,不可以對unique_ptr進行拷貝、賦值等操作,但是可以通過release函式在unique_ptr之間轉移控制權。
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 作為引數和返回值
上述對於拷貝的限制,有兩個特殊情況,即unique_ptr可以作為函式的返回值和引數使用,這時雖然也有隱含的拷貝存在,但是並非不可行的。
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函式,以後再單獨具體細說^_^
unique_ptr deleter
unique_ptr同樣可以設定deleter,和shared_ptr不同的是,它需要在模板引數中指定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所指向的物件,但是卻不增加物件的引用計數。這樣就有可能出現weak_ptr所指向的物件實際上已經被釋放了的情況。因此,weak_ptr有一個lock函式,嘗試取回一個指向物件的shared_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;總結
(1)shared_ptr採用引用計數的方式管理所指向的物件。
(2)shared_ptr可以使用一個new表示式返回的指標進行初始化;但是,不能將一個new表示式返回的指標賦值給shared_ptr。
(3)一旦將一個new表示式返回的指標交由shared_ptr管理之後,就不要再通過普通指標訪問這塊記憶體。
(4)shared_ptr可以通過reset方法重置指向另一個物件,此時原物件的引用計數減一。
(5)可以定製一個deleter函式,用於在shared_ptr釋放物件時呼叫。
(6)unique_ptr對於所指向的物件,是獨佔的。
(7)不可以對unique_ptr進行拷貝、賦值等操作,但是可以通過release函式在unique_ptr之間轉移控制權。
(8)unique_ptr可以作為函式的返回值和引數使用。
(9)unique_ptr同樣可以設定deleter,需要在模板引數中指定deleter的型別。
(10)weak_ptr一般和shared_ptr配合使用。它可以指向shared_ptr所指向的物件,但是卻不增加物件的引用計數。
(11)weak_ptr有一個lock函式,嘗試取回一個指向物件的shared_ptr。
注:另外本人從事線上教育多年,將自己的資料整合建了一個QQ群,對於有興趣一起交流學習c/c++的初學者可以加群:941636044,裡面有大神會給予解答,也會有許多的資源可以供大家學習分享,歡迎大家前來一起學習進步!