【C++11新特性】 C++11智能指針之shared_ptr


C++中的智能指針首先出現在“准”標准庫boost中。隨着使用的人越來越多,為了讓開發人員更方便、更安全的使用動態內存,C++11也引入了智能指針來管理動態對象。在新標准中,主要提供了shared_ptr、unique_ptr、weak_ptr三種不同類型的智能指針。接下來的幾篇文章,我們就來總結一下這些智能指針的使用。

今天,我們先來看看shared_ptr智能指針。

shared_ptr
shared_ptr是一個引用計數智能指針,用於共享對象的所有權也就是說它允許多個指針指向同一個對象。這一點與原始指針一致。

先來一段簡單的代碼,看看shared_ptr的簡單使用:

#include <iostream>
#include <memory>
using namespace std;

class Example
{
public:
Example() : e(1) { cout << "Example Constructor..." << endl; }
~Example() { cout << "Example Destructor..." << endl; }

int e;
};

int main() {

shared_ptr<Example> pInt(new Example());
cout << (*pInt).e << endl;
cout << "pInt引用計數: " << pInt.use_count() << endl;

shared_ptr<Example> pInt2 = pInt;
cout << "pInt引用計數: " << pInt.use_count() << endl;
cout << "pInt2引用計數: " << pInt2.use_count() << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
程序輸出如下:

Example Constructor...
pInt: 1
pInt引用計數: 1
pInt引用計數: 2
pInt2引用計數: 2
Example Destructor...
1
2
3
4
5
6
從上面這段代碼中,我們對shared_ptr指針有了一些直觀的了解。一方面,跟STL中大多數容器類型一樣,shared_ptr也是模板類,因此在創建shared_ptr時需要指定其指向的類型。另一方面,正如其名一樣,shared_ptr指針允許讓多個該類型的指針共享同一堆分配對象。同時shared_ptr使用經典的“引用計數”方法來管理對象資源,每個shared_ptr對象關聯一個共享的引用計數。對於“引用計數”的問題,這里就不展開介紹。有興趣的同學可以上網查找資料,或者看看我的另一篇文章 【Cocos2d-x源碼分析】 Cocos2d-x內存管理解析(Cocos2d-x也使用了“引用計數”方法來實現內存管理)。

對於shared_ptr在拷貝和賦值時的行為,《C++Primer第五版》中有詳細的描述:

每個shared_ptr都有一個關聯的計數值,通常稱為引用計數。無論何時我們拷貝一個shared_ptr,計數器都會遞增。
例如,當用一個shared_ptr初始化另一個shred_ptr,或將它當做參數傳遞給一個函數以及作為函數的返回值時,它
所關聯的計數器就會遞增。當我們給shared_ptr賦予一個新值或是shared_ptr被銷毀(例如一個局部的
shared_ptr離開其作用域)時,計數器就會遞減。一旦一個shared_ptr的計數器變為0,它就會自動釋放自己所管理
的對象。
1
2
3
4
5
對比我們上面的代碼可以看到:當我們將一個指向Example對象的指針交給pInt管理后,其關聯的引用計數為1。接下來,我們用pInt初始化pInt2,兩者關聯的引用計數值增加為2。隨后,函數結束,pInt和PInt2相繼離開函數作用於,相應的引用計數值分別自減1最后變為0,於是Example對象被自動釋放(調用其析構函數)。

接下來,我們完整地介紹一下shared_ptr的常見用法:

1、創建shared_ptr實例
最安全和高效的方法是調用make_shared庫函數,該函數會在堆中分配一個對象並初始化,最后返回指向此對象的share_ptr實例。如果你不想使用make_ptr,也可以先明確new出一個對象,然后把其原始指針傳遞給share_ptr的構造函數。

示例如下:

int main() {

// 傳遞給make_shared函數的參數必須和shared_ptr所指向類型的某個構造函數相匹配
shared_ptr<string> pStr = make_shared<string>(10, 'a');
cout << *pStr << endl; // aaaaaaaaaa

int *p = new int(5);
shared_ptr<int> pInt(p);
cout << *pInt << endl; // 5
}
1
2
3
4
5
6
7
8
9
10
2、訪問所指對象
shared_ptr的使用方式與普通指針的使用方式類似,既可以使用解引用操作符*獲得原始對象進而訪問其各個成員,也可以使用指針訪問符->來訪問原始對象的各個成員。

3、拷貝和賦值操作
我們可以用一個shared_ptr對象來初始化另一個share_ptr實例,該操作會增加其引用計數值。

例如:

int main() {
shared_ptr<string> pStr = make_shared<string>(10, 'a');
cout << pStr.use_count() << endl; // 1

shared_ptr<string> pStr2(pStr);
cout << pStr.use_count() << endl; // 2
cout << pStr2.use_count() << endl; // 2
}
1
2
3
4
5
6
7
8
如果shared_ptr實例p和另一個shared_ptr實例q所指向的類型相同或者可以相互轉換,我們還可以進行諸如p = q這樣賦值操作。該操作會遞減p的引用計數值,遞增q的引用計數值。

例如:

class Example
{
public:
Example(string n) : name(n) { cout << n << " constructor..." << endl; }
~Example() { cout << name << " destructor..." << endl; }

string name;
};

int main() {

shared_ptr<Example> pStr = make_shared<Example>("a object");
shared_ptr<Example> pStr2 = make_shared<Example>("b object");
cout << pStr.use_count() << endl;
cout << pStr2.use_count() << endl;

pStr = pStr2; // 此后pStr和pStr指向相同對象
cout << pStr->name << endl;
cout << pStr2->name << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
輸出如下:

a object constructor...
b object constructor...
1
1
a object destructor...
b object
b object
b object destructor...
1
2
3
4
5
6
7
8
4、檢查引用計數
shared_ptr提供了兩個函數來檢查其共享的引用計數值,分別是unique()和use_count()。

在前面,我們已經多次使用過use_count()函數,該函數返回當前指針的引用計數值。值得注意的是use_count()函數可能效率很低,應該只把它用於測試或調試。

unique()函數用來測試該shared_ptr是否是原始指針唯一擁有者,也就是use_count()的返回值為1時返回true,否則返回false。

示例:

int main() {
shared_ptr<string> pStr = make_shared<string>(10, 'a');
cout << pStr.unique() << endl; // true

shared_ptr<string> pStr2(pStr);
cout << pStr2.unique() << endl; // false;
}
————————————————
版權聲明:本文為CSDN博主「Fred^_^」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/xiejingfa/article/details/50750037


免責聲明!

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



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