寫在前面的總結(jié):
一個shared_ptr對象管理一個指針(new T,在堆空間),多個shared_ptr對象可以管理同一個指針,只有某個shared_ptr對象第一次初始化指針時才執(zhí)行指針的構(gòu)造函數(shù),管理同一個指針的shared_ptr對象個數(shù)稱為引用計數(shù),這個引用計數(shù)保存在每個管理該指針的shared_ptr對象中,當(dāng)引用計數(shù)為0時,這個指針執(zhí)行析構(gòu)函數(shù)釋放;shared_ptr對象也可以管理空指針,此時引用計數(shù)為0。
shared_ptr做為函數(shù)參數(shù)傳遞時,函數(shù)運行期間引用計數(shù)加一,函數(shù)運行完后離開作用域,引用計數(shù)減一。
shared_ptr功能介紹
智能指針和普通指針用法相似,智能指針的本質(zhì)是一個模板類,對普通指針進(jìn)行了封裝,通過在構(gòu)造函數(shù)中初始化分配內(nèi)存,在析構(gòu)函數(shù)中釋放內(nèi)存,達(dá)到自己管理內(nèi)存,不需要手動管理內(nèi)存的效果,避免了忘記釋放內(nèi)存而導(dǎo)致的內(nèi)存泄露。
shared_ptr 是C++11提供的一種智能指針類,可以在任何地方都不使用時自動刪除相關(guān)指針,從而幫助徹底消除內(nèi)存泄漏和懸空指針的問題。
它遵循共享所有權(quán)的概念,即不同的 shared_ptr 對象可以與相同的指針相關(guān)聯(lián),并在內(nèi)部使用引用計數(shù)機制來實現(xiàn)這一點。
每個 shared_ptr 對象在內(nèi)部指向兩個內(nèi)存位置:
1、指向?qū)ο蟮闹羔槨?br> 2、用于控制引用計數(shù)數(shù)據(jù)的指針。
共享所有權(quán)如何在參考計數(shù)的幫助下工作:
1、當(dāng)新的 shared_ptr 對象與指針關(guān)聯(lián)時,則在其構(gòu)造函數(shù)中,將與此指針關(guān)聯(lián)的引用計數(shù)增加1。
2、當(dāng)任何 shared_ptr 對象超出作用域時,則在其析構(gòu)函數(shù)中,它將關(guān)聯(lián)指針的引用計數(shù)減1。如果引用計數(shù)變?yōu)?,則表示沒有其他 shared_ptr 對象與此內(nèi)存關(guān)聯(lián),在這種情況下,它使用delete函數(shù)刪除該內(nèi)存。
shared_ptr是以類模板的方式實現(xiàn)的,shared_ptr(其中 T 表示指針指向的具體數(shù)據(jù)類型)的定義位于頭文件。
shared_ptr提供的接口
shared_ptr是通過引用計數(shù)的方式來實現(xiàn)多個shared_ptr對象之間共享資源。
shared_ptr的存儲指針和引用計數(shù)指針是一一對應(yīng)的,即shared_ptr里存的是存儲指針,對應(yīng)的引用計數(shù)指針就是對stored pointer的加一,因此shared_ptr在其內(nèi)部,給每個資源都維護(hù)著一份計數(shù),用來記錄該份資源被幾個對象共享。
shared_ptr初始化
構(gòu)造函數(shù)初始化
///1.構(gòu)造函數(shù)初始化
//傳入空指針或什么都不傳,構(gòu)造出空智能指針,其初始引用計數(shù)方式為0
std::shared_ptr<int> p0(nullptr);
printf("p0.use_count=%ld\n",p0.use_count());//p0.use_count=0
//構(gòu)造函數(shù)初始化,指向一個存有5這個int類型數(shù)據(jù)的堆內(nèi)存空間
std::shared_ptr<int> p1(new int(5));
printf("p1=%p\n",&p1);//p1=0x7ffc24046f10
printf("p1=%d\n",*p1);//p1=5
printf("p1.use_count=%ld\n",p1.use_count());//1
std::shared_ptr<int> p2(p1);//p1和p2都指向那個存有int型5的堆內(nèi)存空間,堆內(nèi)存的引用次數(shù)會加1
printf("p1.use_count=%ld\n",p1.use_count());//2
printf("p2.use_count=%ld\n",p2.use_count());//2
std::shared_ptr<int> p3=p0;//P0為空,則P3也為空,其引用計數(shù)依然為0
printf("p3.use_count=%ld\n",p3.use_count());//0
//補充
//可以把原始指針傳參構(gòu)造shared_ptr對象,此時原始指針沒有new或沒有初始化賦值,use_count都是1
int *p11 = new int;//如果沒有=new int,下面p12.use_count還是1,但執(zhí)行打印時會crash
//p11 = nullptr;//如果p11賦值為nullptr,下面p12.use_count還是1,但執(zhí)行打印時會crash
std::shared_ptr<int> p12(p11);
printf("p12.use_count=%ld\n",p12.use_count());//1
printf("*p12=%d\n",*p12);//*p12=1345903632(原始指針new了,但沒有初始化,則打印未知值)
std::make_shared 初始化
///2.std::make_shared 初始化
std::shared_ptr<int> p4 = std::make_shared<int>(); //1.定義一個空的智能指針
std::shared_ptr<int> p5= std::make_shared<int>(10);//2.創(chuàng)建指針,并明確指向
auto p6 = std::make_shared<std::vector<int>>();//3.auto關(guān)鍵字代替std::shared_ptr,p8指向一個動態(tài)分配的空vector<int>
reset初始化
///3.reset初始化
//調(diào)用reset(new xxx())重新賦值時,智能指針首先是生成新對象,然后將舊對象的引用計數(shù)減1(當(dāng)然,如果發(fā)現(xiàn)引用計數(shù)為0時,則析構(gòu)舊對象),然后將新對象的指針交給智能指針保管。
std::shared_ptr<int> p7(new int(20));
std::shared_ptr<int> p8(p7);
std::shared_ptr<int> p9(p7);
printf("p9.use_count=%ld\n",p8.use_count());//3
p7.reset(new int(21));//當(dāng)為reset傳遞一個新申請的堆內(nèi)存時,則調(diào)用該函數(shù)的 shared_ptr 對象會獲得該存儲空間的所有權(quán),并且引用計數(shù)的初始值為1;其所指向的原堆內(nèi)存引用計數(shù)減1
printf("p9.use_count=%ld\n",p8.use_count());//2
p9.reset();//當(dāng)reset沒有實參時,該函數(shù)會使當(dāng)前 shared_ptr 所指堆內(nèi)存的引用計數(shù)減 1,同時將當(dāng)前對象重置為一個空指針
if(p9 == nullptr)
printf("p10 == nullptr\n");//p10 == nullptr
printf("p9.use_count=%ld\n",p8.use_count());//1
shared_ptr管理指針的構(gòu)造和析構(gòu)
下面例子介紹shared_ptr是如何管理所指向指針(堆空間)的構(gòu)造和析構(gòu)的。
class book
{
public:
book(int v) {//構(gòu)造函數(shù)
value = v;
std::cout << "cons book value=" <<value<< std::endl;
}
~book() {//析構(gòu)函數(shù)
std::cout << "desc book value=" <<value<< std::endl;
}
int value;
};
{
std::shared_ptr<book> b1(new book(100));//cons book value=100
std::shared_ptr<book> b2(b1);//只增加了引用計數(shù),沒有新增構(gòu)造book,use_count=2
b2.reset(new book(200));//cons book value=200(新構(gòu)造book200,原book100計數(shù)變?yōu)?)
b1.reset(new book(300));//cons book value=300 desc book value=100(新構(gòu)造book300,原book100引用計數(shù)變?yōu)?,執(zhí)行析構(gòu))
printf("end\n");//準(zhǔn)備離開作用域
}
打印,shared_ptr初始化時如果生成了新的指針,則執(zhí)行指針的構(gòu)造函數(shù),如果指向該指針的shared_ptr對象引用計數(shù)為0時,執(zhí)行該指針的析構(gòu)函數(shù);
如果shared_ptr對象超出了作用域,則引用計數(shù)減1,引用計數(shù)為0時執(zhí)行析構(gòu)。
cons book value=100
cons book value=200
cons book value=300
desc book value=100
end
desc book value=200
desc book value=300
shared_ptr獲取原始指針
智能指針提供了get()成員函數(shù),用來執(zhí)行顯示轉(zhuǎn)換,返回智能指針內(nèi)部的原始指針。
std::shared_ptr<int> p10(new int(300));
int *pn = p10.get();
printf("pn=%d\n",*pn);//pn=300
shared_ptr的線程安全
1、shared_ptr不是線程安全的;
2、在多線程下,不能保證new出來一個對象一定能被放入shared_ptr中,也不能保證智能指針管理的引用計數(shù)的正確性;
3、同一個shared_ptr對象可以被多線程同時讀取,不同的shared_ptr對象可以被多線程同時修改,但同一個shared_ptr對象不能被多線程直接修改;
4、在創(chuàng)建一個shared_ptr時,需要使用C++11提供的make_shared模板,make_shared創(chuàng)建shared_ptr只申請一次內(nèi)存,避免了上述錯誤,也提高了性能,同時在讀寫操作時,需要加鎖。
shared_ptr應(yīng)用之enable_shared_from_this
C++11 開始支持 enable_shared_from_this,它是一個模板類,定義在頭文件 ,其原型為:
template< class T > class enable_shared_from_this;
enable_shared_from_this 能讓一個指針(假設(shè)其名為 t ,且已被一個 std::shared_ptr 對象 pt 管理)安全地生成其他額外的 std::shared_ptr 實例(假設(shè)名為 pt1, pt2, … ) ,它們與 pt 共享對象 t 的所有權(quán)。
若一個類 T 繼承 std::enable_shared_from_this ,則會為該類 T 提供成員函數(shù): shared_from_this 。
當(dāng) T 類型對象 t 被一個為名為 pt 的 std::shared_ptr 類對象管理時,調(diào)用 T::shared_from_this 成員函數(shù),將會返回一個新的 std::shared_ptr 對象,它與 pt 共享 t 的所有權(quán)。
使用場景
1、當(dāng)類A被share_ptr管理,且在類A的成員函數(shù)里需要把當(dāng)前類對象作為參數(shù)傳給其他函數(shù)時,就需要傳遞一個指向自身的share_ptr。
2、在異步調(diào)用中,可能會使用到之前存在的變量,為了保證該變量在異步調(diào)用中一直有效,可以傳遞一個指向自身的share_ptr給異步函數(shù),這樣share_ptr所管理的對象就不會析構(gòu)(引用計數(shù)至少>=1,?;睿?。
#include <iostream>
class Good : public std::enable_shared_from_this<Good> // 注意:繼承
{
public:
Good(){std::cout << "Good::Good() called" << std::endl; }
std::shared_ptr<Good> getptr() {
return shared_from_this();
}
~Good() { std::cout << "Good::~Good() called" << std::endl; }
};
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow)
{
ui->setupUi(this);
std::shared_ptr<Good> gp1(new Good());
std::cout << "gp1.use_count() = " << gp1.use_count() << std::endl;//1
std::shared_ptr<Good> gp2 = gp1->getptr();
std::cout << "gp1.use_count() = " << gp1.use_count() << std::endl;//2
}
打印
Good::Good() called
gp1.use_count() = 1
gp1.use_count() = 2
Good::~Good() called
weak_ptr
std::weak_ptr 是一種智能指針,通常不單獨使用,只能和 shared_ptr 類型指針搭配使用,可以視為 shared_ptr 指針的一種輔助工具。借助 weak_ptr 類型指針可以獲取 shared_ptr 指針的一些狀態(tài)信息,比如有多少指向相同的 shared_ptr 指針、通過expired()判斷shared_ptr 指針指向的堆內(nèi)存是否已經(jīng)被釋放等等,還可以解決shared_ptr 循環(huán)引用的問題。
weak_ptr可以從一個shared_ptr或者另一個weak_ptr對象構(gòu)造,獲得資源的觀測權(quán)。但weak_ptr沒有共享資源,它的構(gòu)造不會引起指針引用計數(shù)的增加。使用weak_ptr的成員函數(shù)use_count()可以觀測資源的引用計數(shù),另一個成員函數(shù)expired()的功能等價于use_count()==0,但更快。表示被觀測的資源(也就是shared_ptr的管理的資源)已經(jīng)不復(fù)存在。
lock()從被觀測的shared_ptr獲得一個可用的shared_ptr對象, 從而操作資源。但當(dāng)expired()==true的時候,lock()函數(shù)將返回一個存儲空指針的shared_ptr。
std::shared_ptr<int> sh_ptr = std::make_shared<int>(10);//創(chuàng)建一個智能指針
printf("sh_ptr.use_count()=%ld\n",sh_ptr.use_count());//sh_ptr.use_count()=1
std::weak_ptr<int> wp(sh_ptr);//構(gòu)造 weak_ptr,不會增加智能指針的引用計數(shù)
printf("sh_ptr.use_count()=%ld\n",sh_ptr.use_count());//sh_ptr.use_count()=1
printf("wp.use_count() =%ld\n",wp.use_count() );// wp.use_count() =1
if(!wp.expired())
{ // 檢查sh_ptr是否還有效
std::shared_ptr<int> sh_ptr2 = wp.lock(); //使用 weak_ptr 構(gòu)造一個智能指針,引用計數(shù)+1
*sh_ptr = 100;
printf("wp.use_count() =%ld\n",wp.use_count() );// wp.use_count() =2
}
} //delete memory
std::weak_ptr<int> wp;
{
std::shared_ptr<int> sh_ptr = std::make_shared<int>(10);
wp = sh_ptr;//構(gòu)造 weak_ptr
printf("wp.expired()=%d\n",wp.expired());// wp.expired()=0,引用對象還沒刪除
} //delete memory
printf("wp.expired()=%d\n",wp.expired()); // wp.expired()=1,引用對象已經(jīng)刪除
sh_ptr.use_count()=1
sh_ptr.use_count()=1
wp.use_count() =1
wp.use_count() =2
wp.expired()=0
wp.expired()=1
循環(huán)引用問題
weak_ptr的一個作用是解決share_ptr的循環(huán)引用問題。如下面代碼所示,class AA中含有指向class BB的shared指針, class BB 中含有指向class AA的shared指針,這樣形成了循環(huán)引用。m_bb_ptr和m_aa_ptr的強引用計數(shù)永遠(yuǎn)大于等于1
class BB;
class AA
{
public:
AA() { printf("AA::AA() called\n"); }
~AA() { printf( "AA::~AA() called\n"); }
std::shared_ptr<BB> m_bb_ptr;//正確用法是使用weak_ptr,可以正常析構(gòu)
};
class BB
{
public:
BB() { printf("BB::BB() called\n" ); }
~BB() { printf("BB::~BB() called\n" ); }
std::shared_ptr<AA> m_aa_ptr;//正確用法是使用weak_ptr,可以正常析構(gòu)
};
std::shared_ptr<AA> ptr_a(new AA);
std::shared_ptr<BB> ptr_b(new BB);
printf( "ptr_a use_count: %ld\n" , ptr_a.use_count() );
printf( "ptr_b use_count: %ld\n" , ptr_b.use_count() );
//下面兩句導(dǎo)致了AA與BB的循環(huán)引用,結(jié)果就是AA和BB對象都不會析構(gòu)
ptr_a->m_bb_ptr = ptr_b;
ptr_b->m_aa_ptr = ptr_a;
printf( "ptr_a use_count: %ld\n" ,ptr_a.use_count() );
printf( "ptr_b use_count: %ld\n" ,ptr_b.use_count() );
AA::AA() called
BB::BB() called
ptr_a use_count: 1
ptr_b use_count: 1
ptr_a use_count: 2
ptr_b use_count: 2
智能指針shared_ptr開發(fā)注意事項
均為代碼實測記錄。
基本使用
1、std::shared_ptr p1調(diào)用reset后use_count減1,p1 == nullptr。
2、一個智能指針std::shared_ptr p1引用計數(shù)為0或者是空指針,則p1 == nullptr,不能調(diào)用類接口,但可以調(diào)用p1.use_count(),返回0。
3、不能使用delete釋放std::shared_ptr的.get原始指針,程序會crash。
4、std::shared_ptr被push_back到std::vector數(shù)組,use_count加1,如果數(shù)組clear或智能指針成員被erase,use_count減1。
shared_from_this
1、類繼承于std::enable_shared_from_this<>后,可以定義接口返回shared_from_this,這樣函數(shù)就可以傳遞.get原始指針,在函數(shù)體內(nèi)調(diào)用接口返回shared_from_this,在函數(shù)體內(nèi)use_count加1。
2、如果使用shared_from_this().get()傳參給void*,在函數(shù)執(zhí)行過程中use_count加1,執(zhí)行完成后use_count減1。
3、如果智能指針已釋放,原始指針失效,此時通過原始指針調(diào)用接口返回shared_from_this時會拋出異常,可以捕獲異常:try {pUdpSptr = pUdp->get_shared_ptr();} catch (std::exception &ex) {LogError ( " %s", ex.what()); }。
智能指針傳參
1、std::shared_ptr可以通過.get獲取原始指針,傳參給void*類型,但引用計數(shù)不會增加。
2、std::shared_ptr p1做為普通參數(shù)傳遞,如果p1是空指針,則傳遞過程中use_count始終為0;如果p1不是空指針,執(zhí)行過程中use_count加1,執(zhí)行完成后use_count減1。
3、std::shared_ptr做為引用傳遞,如果p1是空指針,則傳遞過程中use_count始終為0;如果p1不是空指針,函數(shù)執(zhí)行前后use_count也不會改變。
4、std::shared_ptr傳參,如果要改變值,可以傳shared_from_this,做為普通參數(shù)傳遞,執(zhí)行過程中use_count加1,增加安全行,也可以改變原始指針的值。
5、lambda表達(dá)式傳參智能指針,值傳遞會增加引用計數(shù),引用傳遞不會增加引用計數(shù);值傳遞,lambda表達(dá)生成的匿名函數(shù)func=nullptr后,引用計數(shù)減一。
智能指針做為返回值
1、std::shared_ptr做為普通返回值,use_count加1,但是如果沒用定義std::shared_ptr變量接收返回值,use_count不變。
2、返回std::shared_ptr的引用,use_count和普通返回值一樣;不能返回局部變量或臨時變量的引用。
3、如果要返回空智能指針,可以return std::shared_ptr<>(nullptr)。文章來源:http://www.zghlxwxcb.cn/news/detail-437141.html
智能指針的構(gòu)造和析構(gòu)
1、新增std::shared_ptr變量時,如果生成了新的原始指針,則執(zhí)行構(gòu)造函數(shù)。
2、新增std::shared_ptr變量時,如果只增加了引用計數(shù),沒有生成新指針,則不會執(zhí)行構(gòu)造函數(shù)。
3、當(dāng)引用計數(shù)變?yōu)?時,執(zhí)行析構(gòu)函數(shù)。
4、一句話,use_count從0變?yōu)?時執(zhí)行構(gòu)造,use_count從1變?yōu)?時執(zhí)行析構(gòu)。文章來源地址http://www.zghlxwxcb.cn/news/detail-437141.html
到了這里,關(guān)于C++智能指針shared_ptr用法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!