1. 程式人生 > >c++11 智能指針 unique_ptr、shared_ptr與weak_ptr

c++11 智能指針 unique_ptr、shared_ptr與weak_ptr

del 最終 err fin 導致 cto 允許 定義 ptr

c++11 智能指針 unique_ptr、shared_ptr與weak_ptr

C++11中有unique_ptr、shared_ptr與weak_ptr等智能指針(smart pointer),定義在<memory>中。

可以對動態資源進行管理,保證任何情況下,已構造的對象最終會銷毀,即它的析構函數最終會被調用。

unique_ptr

unique_ptr持有對對象的獨有權,同一時刻只能有一個unique_ptr指向給定對象(通過禁止拷貝語義、只有移動語義來實現)。

unique_ptr指針本身的生命周期:從unique_ptr指針創建時開始,直到離開作用域。

離開作用域時,若其指向對象,則將其所指對象銷毀(默認使用delete操作符,用戶可指定其他操作)。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <map>


void mytest()
{
    std::unique_ptr<int> up1(new int(11));   // 無法復制的unique_ptr
    //unique_ptr<int> up2 = up1;        // err, 不能通過編譯
    std::cout << *up1 << std::endl;   //
11 std::unique_ptr<int> up3 = std::move(up1); // 現在p3是數據的唯一的unique_ptr std::cout << *up3 << std::endl; // 11 //std::cout << *up1 << std::endl; // err, 運行時錯誤 up3.reset(); // 顯式釋放內存 up1.reset(); // 不會導致運行時錯誤 //std::cout << *up3 << std::endl;
// err, 運行時錯誤 std::unique_ptr<int> up4(new int(22)); // 無法復制的unique_ptr up4.reset(new int(44)); //"綁定"動態對象 std::cout << *up4 << std::endl; // 44 up4 = nullptr;//顯式銷毀所指對象,同時智能指針變為空指針。與up4.reset()等價 std::unique_ptr<int> up5(new int(55)); int *p = up5.release(); //只是釋放控制權,不會釋放內存 std::cout << *p << std::endl; //cout << *up5 << endl; // err, 運行時錯誤 delete p; //釋放堆區資源 return; } int main() { mytest(); system("pause"); return 0; }

shared_ptr

shared_ptr允許多個該智能指針共享第“擁有”同一堆分配對象的內存,這通過引用計數(reference counting)實現,會記錄有多少個shared_ptr共同指向一個對象,一旦最後一個這樣的指針被銷毀,也就是一旦某個對象的引用計數變為0,這個對象會被自動刪除。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <map>


void mytest()
{
    std::shared_ptr<int> sp1(new int(22));
    std::shared_ptr<int> sp2 = sp1;
    std::cout << "cout: " << sp2.use_count() << std::endl; // 打印引用計數

    std::cout << *sp1 << std::endl;
    std::cout << *sp2 << std::endl;

    sp1.reset(); // 顯示讓引用計數減一
    std::cout << "count: " << sp2.use_count() << std::endl; // 打印引用計數

    std::cout << *sp2 << std::endl; // 22

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

weak_ptr

weak_ptr是為配合shared_ptr而引入的一種智能指針來協助shared_ptr工作,它可以從一個shared_ptr或另一個weak_ptr對象構造,它的構造和析構不會引起引用計數的增加或減少。沒有重載 * 和 -> 但可以使用lock獲得一個可用的shared_ptr對象

weak_ptr的使用更為復雜一點,它可以指向shared_ptr指針指向的對象內存,卻並不擁有該內存,而使用weak_ptr成員lock,則可返回其指向內存的一個share_ptr對象,且在所指對象內存已經無效時,返回指針空值nullptr。

註意:weak_ptr並不擁有資源的所有權,所以不能直接使用資源。
可以從一個weak_ptr構造一個shared_ptr以取得共享資源的所有權。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <map>

void check(std::weak_ptr<int> &wp)
{
    std::shared_ptr<int> sp = wp.lock(); // 轉換為shared_ptr<int>
    if (sp != nullptr)
    {
        std::cout << "still: " << *sp << std::endl;
    } 
    else
    {
        std::cout << "still: " << "pointer is invalid" << std::endl;
    }
}


void mytest()
{
    std::shared_ptr<int> sp1(new int(22));
    std::shared_ptr<int> sp2 = sp1;
    std::weak_ptr<int> wp = sp1; // 指向shared_ptr<int>所指對象

    std::cout << "count: " << wp.use_count() << std::endl; // count: 2
    std::cout << *sp1 << std::endl; // 22
    std::cout << *sp2 << std::endl; // 22
    check(wp); // still: 22
    
    sp1.reset();
    std::cout << "count: " << wp.use_count() << std::endl; // count: 1
    std::cout << *sp2 << std::endl; // 22
    check(wp); // still: 22

    sp2.reset();
    std::cout << "count: " << wp.use_count() << std::endl; // count: 0
    check(wp); // still: pointer is invalid

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

c++11 智能指針 unique_ptr、shared_ptr與weak_ptr