代碼1
int main(){
//裸指標,手動開辟,需要自己釋放,如果忘記了或者因為
//程式邏輯導致p沒有釋放,那么就會導致記憶體泄漏
int *p=new int(10);
if(***){
retur -1;
}
delete p;
return 0;
}
有沒有什么辦法幫我們管理指標,確保資源釋放?
智能指標
利用堆疊上的物件出作用域時自動析構的特征,來做到資源的自動釋放
問題:是否可以在堆上創建裸指標?語法上沒有問題,但是我們正式希望
堆疊上物件出作用域能自動析構的特征來達到自動管理指標的目的,如果
將智能指標創建在堆上,那又和原來的裸指標使用遇到的問題是一樣的了
需要手動delete
代碼2
#include <iostream>
using namespace std;
template<typename T>
class MySmartPtr1 {
public:
MySmartPtr1(T * ptr=nullptr) : _mptr(ptr) { }
~MySmartPtr1() {
delete _mptr;
_mptr = nullptr;
}
T & operator*() { return *_mptr; }//回傳的 是 & , 需要修改值
T * operator->() { return _mptr; }
private:
T * _mptr;
};
int main() {
MySmartPtr1<int> ptr(new int(10));
*ptr= 200;
return 0;
}
代碼2的問題
int main() {
MySmartPtr1<int> ptr(new int(10));
//使用ptr 拷貝構造ptr2,默認的拷貝構造方式是值拷貝,所以底層
//_mptr指標 指向的是同一塊記憶體,那么ptr2 和ptr析構的時候就會有問題了,兩次析構同一片記憶體
MySmartPtr1<int> ptr2(ptr);
*mptr = 200;
return 0;
}
如何解決呢?
1:不帶參考計數的智能指標
auto_ptr C++庫提供
C++11 新標準
scoped_ptr
unique_ptr
代碼 關于 auto_ptr
int main() {
auto_ptr<int> ptr1(new int(100));
auto_ptr<int> ptr2(ptr1);
*ptr2 = 200;
cout<<*ptr1<<endl;//執行報錯,原因見下圖
return 0;
}

現在不推薦使用auto_ptr
容器中推薦用auto_ptr嗎? vector<auto_ptr
代碼關于 scoped_ptr
int main() {
scope_ptr的處理方式
scope_ptr<int>(const scope_ptr<int> & src)=delete;//通過直接和諧掉這兩個方法
scope_ptr<int> & operator=(const scope_ptr<int> & src))=delete;//通過直接和諧掉這兩個方法
return 0;
}
所以scoped_ptr使用的也很少
代碼關于 unique_ptr
int main() {
unique_ptr的處理方式
unique_ptr<int>(const unique_ptr<int> & src)=delete;//通過直接和諧掉這兩個方法
unique_ptr<int> & operator=(const unique_ptr<int> & src))=delete;//通過直接和諧掉這兩個方法
unique_ptr<int> ptr1(new int(100));
unique_ptr<int> ptr2(ptr1);//左值的拷貝構造和賦值函式已經被屏蔽了,所以編譯報錯,"嘗試使用已經洗掉的函式", 要改成如下!!!
unique_ptr<int> ptr1(new int(100));
unique_ptr<int> ptr2(std::move(ptr1));//編譯OK,為什么可以呢?因為unique_ptr提供了右值參考的拷貝構造和右值參考的賦值函式,如下
unique_ptr<int>(const unique_ptr<int> && src){};
unique_ptr<int> & operator=(const unique_ptr<int> && src)){};
return 0;
}
//推薦使用
2:帶參考計數的智能指標(share_ptr,weak_ptr)
帶參考計數的好處:多個智能指標可以管理同一個資源
帶參考計數:給每一個物件資源,匹配一個參考計數,
智能指標參考一個資源的時候,給這個資源參考計數加1
當這個智能指標出作用域不再使用資源的時候,給這個資源參考計數-1,當參考計數不為0的時候,還不能析構這個資源,
當參考計數為0的時候,說明已經沒有外部資源使用這個資源了,那么就可以析構這個資源了
代碼3 簡單實作share_ptr
#include <iostream>
using namespace std;
template<typename T>
class RefCount {
public:
RefCount(T * pSrc = https://www.cnblogs.com/erichome/archive/2022/12/02/nullptr, int refCount = 0):_pSrc(pSrc),_refCount(refCount) {
}
void addCount() { this->_refCount++; }
void deleltCount() { --this->_refCount; }
int refCount() { return this->_refCount; }
private:
T * _pSrc;
int _refCount = 0;
};
template
class MySmartPtr2 {
public:
//新創建的智能指標,默認計數器為1
MySmartPtr2 (T * mptr=nullptr): _mptr(mptr){
_pRef = new RefCount(_mptr,1);
}
//拷貝構造
MySmartPtr2(const MySmartPtr2 & _rval) {
//兩個智能指標指向相同的資源
this->_mptr = _rval._mptr;
this->_pRef = _rval._pRef;
this->_pRef->addCount();
}
//賦值多載
MySmartPtr2 & operator=(const MySmartPtr2 & _rval) {
if (this == &_rval) { retur *this; }
else {
this->_pRef->deleltCount();
int currentCount = this->_pRef->refCount();
if (currentCount == 0) {
delete this->_mptr;//銷毀指向的資源
this->_mptr = nullptr;
delete _pRef;
_rPef = nullptr;
}
this->_pRef = _rval._pRef;
this->_mptr = _rval._mptr;
this->_pRef->addCount();
return *this;
}
}
~MySmartPtr2() {
this->_pRef->deleltCount();
int currentCount = this->_pRef->refCount();
if (currentCount == 0) {
delete this->_mptr;//銷毀指向的資源
this->_mptr = nullptr;
delete _pRef;
_pRef = nullptr;
}
}
int getRefCount() { return this->_pRef->refCount(); }
private:
T * _mptr;
RefCount * _pRef;
};
int main() {
MySmartPtr2 ms1(new int(100)) ;
{
MySmartPtr2 ms2(ms1);
cout <<"RefCount=" << ms1.getRefCount() << endl;
MySmartPtr2<int> ms3(ms1);
cout << "RefCount=" << ms1.getRefCount() << endl;
}
cout << "RefCount=" << ms1.getRefCount() << endl;
system("pause");
return 0;
}
share_ptr: 強智能指標,可以改變資源的參考計數
weak_ptr: 弱智能指標,不會改變資源的參考計數
強智能指標:回圈參考(交叉參考)是什么問題?什么結果?怎么解決?
交叉參考代碼
class A{
pubic:
A(){cout<<"A()"<<endl;}
~A(){cou<<"~A()"<<endl;}
share_ptr<B> _ptrb;
}
class B{
pubic:
B(){cout<<"B()"<<endl;}
~B(){cou<<"~B()"<<endl;}
share_ptr<A> _ptrb;
}
int main(){
share_ptr<A> pa(new A());
share_ptr<B> pb(new B());
pa->_ptrb=pb;
pb->_ptra=pa;
cout<<pa.use_count()<<endl;// 2
cout<<pb.use_count()<<endl;// 2
}

出main函式的時候,
第1步 先析構pb,pb在析構的時候,發現除了自己參考B物件,其他地方(A物件內)還有參考B物件的,所以 B資源無法釋放
第2步,再析構pa,pa在析構的時候,發現除了自己參考A物件,其他地方(B物件內)還有參考A物件的,所以 導致A物件也無法釋放
出了函式這兩堆上的資源都沒有被釋放掉,泄漏!
上面代碼造成new出來的資源無法釋放!!資源泄漏問題
解決:
定義物件的時候,用強智能指標,參考物件的地方用弱智能指標
class A{
pubic:
A(){cout<<"A()"<<endl;}
~A(){cou<<"~A()"<<endl;}
void testA(){
cout<<"A testA() Function"<<endl;
}
weak_ptr<B> _ptrb;
}
class B{
pubic:
B(){cout<<"B()"<<endl;}
~B(){cou<<"~B()"<<endl;}
void function(){
share_ptr<A> _tp=_ptrb.lock();//提升方法
if(_tp!=nullptr){
_tp->testA();
}
}
weak_ptr<A> _ptrb; //weak_ptr 弱智能指標,不會改變參考計數
}
int main(){
share_ptr<A> pa(new A());
share_ptr<B> pb(new B());
pa->_ptrb=pb;
pb->_ptra=pa;
pb.function();
cout<<pa.use_count()<<endl;// 2
cout<<pb.use_count()<<endl;// 2
}
share_ptr
share_ptr是C++11新添加的智能指標,它限定的資源可以被多個指標共享,
只有指向動態分配的物件的指標才能交給 shared_ptr 物件托管,將指向普通區域變數、全域變數的指標交給 shared_ptr 托管,編譯時不會有問題,但程式運行時會出錯,因為不能析構一個并沒有指向動態分配的記憶體空間的指標
weak_ptr
weak_ptr是一種用于解決shared_ptr相互參考時產生死鎖問題的智能指標,如果有兩個shared_ptr相互參考,那么這兩個shared_ptr指標的參考計數永遠不會下降為0,資源永遠不會釋放,weak_ptr是對物件的一種弱參考,它不會增加物件的use_count,weak_ptr和shared_ptr可以相互轉化,shared_ptr可以直接賦值給weak_ptr,weak_ptr也可以通過呼叫lock函式來獲得shared_ptr,
weak_ptr指標通常不單獨使用,只能和 shared_ptr 型別指標搭配使用,將一個weak_ptr系結到一個shared_ptr不會改變shared_ptr的參考計數,一旦最后一個指向物件的shared_ptr被銷毀,物件就會被釋放,即使有weak_ptr指向物件,物件也還是會被釋放,
weak_ptr并沒有多載operator->和operator *運算子,因此不可直接通過weak_ptr使用物件,典型的用法是呼叫其lock函式來獲得shared_ptr示例,進而訪問原始物件,
share_ptr 和 weak_ptr 是執行緒安全的.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/539018.html
標籤:其他
下一篇:序章-準備作業
