C++11 新特性之智能指針(shared_ptr, unique_ptr, weak_ptr)


 這是C++11新特性介紹的第五部分,涉及到智能指針的相關內容(shared_ptr, unique_ptr, weak_ptr)。

shared_ptr

shared_ptr 基本用法

       shared_ptr采用引用計數的方式管理所指向的對象。當有一個新的shared_ptr指向同一個對象時(復制shared_ptr等),引用計數加1。當shared_ptr離開作用域時,引用計數減1。當引用計數為0時,釋放所管理的內存。
       這樣做的好處在於解放了程序員手動釋放內存的壓力。之前,為了處理程序中的異常情況,往往需要將指針手動封裝到類中,通過析構函數來釋放動態分配的內存;現在這一過程就可以交給shared_ptr去做了。
       一般我們使用make_shared來獲得shared_ptr。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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表達式返回的指針進行初始化。

1
2
3
4
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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;

 

       上面的程序片段會輸出:

1
2
3
4
5
in process use_count: 2
v5: 1024
in process use_count: 1
v6: 0
cout<<"v6: "<<v6<<endl;

 

       可以看到,第二次process p6時,shared_ptr的引用計數為1,當離開process的作用域時,會釋放對應的內存,此時p6成為了懸掛指針。所以,一旦將一個new表達式返回的指針交由shared_ptr管理之后,就不要再通過普通指針訪問這塊內存!

shared_ptr.reset

       shared_ptr可以通過reset方法重置指向另一個對象,此時原對象的引用計數減一。

1
2
3
4
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函數,用於在shared_ptr釋放對象時調用。

1
2
3
4
5
6
7
8
9
10
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之間轉移控制權。

1
2
3
4
5
6
7
8
9
10
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可以作為函數的返回值和參數使用,這時雖然也有隱含的拷貝存在,但是並非不可行的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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這個利器,不然寫起來好麻煩。

1
2
3
4
5
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。

1
2
3
4
5
6
7
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。

https://greedysky.github.io/2016/08/20/C++11%20%E6%96%B0%E7%89%B9%E6%80%A7%E4%B9%8B%E6%99%BA%E8%83%BD%E6%8C%87%E9%92%88/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM