智能指針實現
原來的內存管理代碼
int main()
{
int *ptr = new(nothrow) int(0); //關閉異常
if(!ptr)
{
cout << "new fails."
return 0;
}
if(!check()) ///有校驗 ,拋出異常,每次都得管理內存
{
delete ptr; //釋放
ptr = nullptr; //防止空懸指針
throw exception();
}
delete ptr; //釋放
ptr = nullptr; //防止空懸指針
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
使用引用計數,實現智能指針
1.構造函數中計數初始化為1,拷貝構造函數中計數值加1;
2.析構函數中引用計數減一;
3.賦值運算符中,左邊的對象引用計數減一,右邊的對象引用計數加一;
4.在賦值運算符和析構函數中,如果減一後為0,則調用delete釋放對象。
/////////////////////////////////////////////////////////////////////////////////////////////
智能指針即為封裝好的,帶計數的指針,需要實現 構造函數,拷貝構造,賦值構造,析構
#include <iostream>
using namespace std;
template<class T> //模板類T,智能指針即為封裝好的,帶計數的指針
class SmartPtr
{
public:
SmartPtr(T *p); //構造函數,參數是普通指針
SmartPtr(const SmartPtr<T> &orig); // 拷貝構造 ,淺拷貝
SmartPtr<T>& operator=(const SmartPtr<T> &rhs); // 賦值操作符號,淺拷貝
~SmartPtr() ; //析構函數
private:
T * ptr; // 指針
int *use_count; // 將use_count聲明成指針是為了方便對其的遞增或遞減操作
};
template<class T>
SmartPtr<T>::SmartPtr(T *p) : ptr(p)
{
try
{
use_count = new int(1); //分配計數內存
}
catch (...)
{
delete ptr;
ptr = nullptr;
use_count = nullptr;
cout << "Allocate memory for use_count fails." << endl;
exit(1); //退出程序
}
cout << "Constructor is called!" << endl;
}
template<class T>
SmartPtr<T>::~SmartPtr()
{
// 只在最後一個對象引用ptr時才釋放內存
if (--(*use_count) == 0)
{
delete ptr;
delete use_count;
ptr = nullptr;
use_count = nullptr;
cout << "Destructor is called!" << endl;
}
}
template<class T>
SmartPtr<T>::SmartPtr( const SmartPtr<T> &orig)
{
ptr = orig.ptr;
use_count = orig.use_count;
++(*use_count);
cout << "Copy constructor is called!" << endl;
}
// 重載等號函數不同於復制構造函數,即等號左邊的對象可能已經指向某塊內存。
// 這樣,我們就得先判斷左邊對象指向的內存已經被引用的次數。如果次數為1,
// 表明我們可以釋放這塊內存;反之則不釋放,由其他對象來釋放。
template<class T>
SmartPtr<T>& SmartPtr<T>::operator = (const SmartPtr<T> &rhs)
{
//這句話如果放在最後面,那麽 rhs=rhs(計數為1的時候),將會釋放內存
++(*rhs.use_count);
// 將左操作數對象的使用計數減1,若該對象的使用計數減至0,則刪除該對象
if (--(*use_count) == 0)
{
delete ptr;
delete use_count;
cout << "Left side object is deleted!" << endl;
}
ptr = rhs.ptr;
use_count = rhs.use_count;
cout << "Assignment operator overloaded is called!" << endl;
return *this;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
使用方法
指向單個元素的 智能指針,會有默認內存釋放器,也可自己定義
shared_ptr<string> str_point ( new string("jutta"),
// 自定義析構函數,lamada表達式
[](string *p)
{
cout << "delete " << *p << endl;
delete p;
}
);
指向數組元素的 智能指針,需要自己定義內存釋放函數
shared_ptr<int> int_point ( new int[10],
// 自定義析構函數,lamada表達式
[](int *p)
{
delete[] p;
}
);
也可以使用unique_ptr的default_delete函數
shared_ptr<int> int_point (new int[10], default_delete <int [] > () );
share_prt與weak_ptr的區別?
智能指針實現