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

公衆號回覆:666,領取學習資源大禮包

作者:Fred^_^   原文:

https://blog.csdn.net/Xiejingfa/article/details/50750037

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;
}

程序輸出如下:

Example Constructor...
pInt: 1
pInt引用計數: 1
pInt引用計數: 2
pInt2引用計數: 2
Example Destructor...

從上面這段代碼中,我們對shared_ptr指針有了一些直觀的瞭解。

一方面,跟STL中大多數容器類型一樣,shared_ptr也是模板類,因此在創建shared_ptr時需要指定其指向的類型。

另一方面,正如其名一樣,shared_ptr指針允許讓多個該類型的指針共享同一堆分配對象。

同時shared_ptr使用經典的“引用計數”方法來管理對象資源,每個shared_ptr對象關聯一個共享的引用計數。

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

每個shared_ptr都有一個關聯的計數值,通常稱爲引用計數。無論何時我們拷貝一個shared_ptr,計數器都會遞增。

例如,當用一個shared_ptr初始化另一個shred_ptr,或將它當做參數傳遞給一個函數以及作爲函數的返回值時,它 所關聯的計數器就會遞增。

當我們給shared_ptr賦予一個新值或是shared_ptr被銷燬(例如一個局部的 shared_ptr離開其作用域)時,計數器就會遞減。

一旦一個shared_ptr的計數器變爲0,它就會自動釋放自己所管理 的對象。

對比我們上面的代碼可以看到:當我們將一個指向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
}

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
}

如果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;
}

輸出如下:

a object constructor...
b object constructor...
1
1
a object destructor...
b object
b object
b object destructor...

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;
}

推薦閱讀:

關於多線程,你必須知道的那些玩意兒

移動端技術交流喊你入羣啦~~~

推薦幾個堪稱教科書級別的 Android 音視頻入門項目

音視頻開發入門必備之基礎知識

覺得不錯,點個在看唄~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章