為了解決C++內存洩漏的問題,C++11引入了智能指針(Smart Pointer)。
智能指針的原理是,接受一個申請好的內存地址,構造一個保存在棧上的智能指針對象,當程序退出棧的作用域范圍後,由於棧上的變量自動被銷毀,智能指針內部保存的內存也就被釋放掉了,除非將智能指針保存起來。
C++11提供了三種智能指針:std::shared_ptr, std::unique_ptr, std::weak_ptr,使用時需添加頭文件<memory>。
shared_ptr使用引用計數,每一個shared_ptr的拷貝都指向相同的內存。每使用他一次,內部的引用技術加1,每析構一次,內部的引用計數減1,減為0時,刪除所指向的堆內存。
可以通過構造函數、std::make_shared<T>輔助函數和reset方法來初始化shared_ptr:
std::shared_ptr<int> p(new int(1));
std::shared_ptr<int> p2 = p;
std::shared_ptr<int> p3 = std::make_shared<int>(5);
std::shared_ptr<int> ptr;
ptr.reset(new int(1));
if (ptr) {
cout << "ptr is not null";
}
注意,不能將一個原始指針直接賦值給一個智能指針,如下所示,原因是一個是類,一個是指針。
std::shared_ptr<int> p4 = new int(1);// error
當智能指針中有值得時候,調用reset會使引用計數減1.
std::shared_ptr<int> p4(new int(5));
int *pInt = p4.get();
智能指針可以指定刪除器,當智能指針的引用計數為0時,自動調用指定的刪除器來釋放內存。設置指定刪除器的一個原因是std::shared_ptr默認刪除器不支持數組對象,這一點需要注意。
2. 使用shared_ptr需要注意的問題
但凡一些高級的用法,使用時都有不少陷阱。
int *p5 = new int;
std::shared_ptr<int> p6(p5);
std::shared_ptr<int> p7(p5);// logic error
function(shared_ptr<int>(new int), g());
struct AStruct;
struct BStruct;
struct AStruct {
std::shared_ptr<BStruct> bPtr;
~AStruct() { cout << "AStruct is deleted!"<<endl; }
};
struct BStruct {
std::shared_ptr<AStruct> APtr;
~BStruct() { cout << "BStruct is deleted!" << endl; }
};
void TestLoopReference()
{
std::shared_ptr<AStruct> ap(new AStruct);
std::shared_ptr<BStruct> bp(new BStruct);
ap->bPtr = bp;
bp->APtr = ap;
}