下面我们先分析一下下面这段程序有没有什么问题?
#include <vector>
void _MergeSort(int* a, int left, int right, int* tmp){
if (left >= right)
return;
int mid = left + ((right - left) >> 1);
// [left, mid]
// [mid+1, right]
_MergeSort(a, left, mid, tmp);
_MergeSort(a, mid + 1, right, tmp);
int begin1 = left, end1 = mid;
int begin2 = mid + 1, end2 = right;
int index = left;
while (begin1 <= end1 && begin2 <= end2){
if (a[begin1] < a[begin2])
tmp[index++] = a[begin1++];
else
tmp[index++] = a[begin2++];
}
while (begin1 <= end1)
tmp[index++] = a[begin1++];
while (begin2 <= end2)
tmp[index++] = a[begin2++];
memcpy(a + left, tmp + left, sizeof(int)*(right - left + 1));
}
void MergeSort(int* a, int n){
int* tmp = (int*)malloc(sizeof(int)*n);
_MergeSort(a, 0, n - 1, tmp);
// 这里假设处理了一些其他逻辑
vector<int> v(1000000000, 10);
// ...
// free(tmp);
}
int main(){
int a[5] = { 4, 5, 2, 3, 1 };
MergeSort(a, 5);
return 0;
}
问题分析:
RAII是一种利用对象生命周期来控制程序资源(内存,文件句柄,互斥量等)的简单技术
在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内保持始终有效,最后在对象西沟的时候释放资源。借此,我们实际上把管理一份资源的责任托管给一个对象,这样做有两个好处:
// 使用RAII思想设计的SmartPtr类
template<class T>
class SmartPtr {
public:
SmartPtr(T* ptr = nullptr)
: _ptr(ptr)
{}
~SmartPtr()
{
if(_ptr)
delete _ptr;
}
private:
T* _ptr;
};
void MergeSort(int* a, int n)
{
int* tmp = (int*)malloc(sizeof(int)*n);
// 讲tmp指针委托给了sp对象,用时老师的话说给tmp指针找了一个可怕的女朋友!天天管着你,直到你go
//die^^
SmartPtr<int> sp(tmp);
// _MergeSort(a, 0, n - 1, tmp);
// 这里假设处理了一些其他逻辑
vector<int> v(1000000000, 10);
// ...
}
int main()
{
try {
int a[5] = { 4, 5, 2, 3, 1 };
MergeSort(a, 5);
}
catch(const exception& e)
{
cout<<e.what()<<endl;
}
return 0;
}
上述的SmartPtr还不能将其称为智能指针,因为它还不具有指针的行为。指针可以解引用,也可以通过->去访问所指空间中的内容,因此:AutoPtr模板类中还得需要将 、->重载下,才可让其像指针一样去使用。*
总结一下智能指针的原理:
实现原理:管理权转移的思想,下面简化模拟实现一份AutoPtr来了解它的原理
#include <iostream>
#include <memory>
using namespace std;
template<class T>
class AutoPtr{
public:
AutoPtr(T* ptr = nullptr)
:_ptr(ptr)
{}
~AutoPtr(){
if(_ptr)
delete _ptr;
}
//一旦发生拷贝,将ap中资源转移到当前对象中,然后令ap与其所管理资源断开联系
//这样就解决了一块空间被多个对象使用造成的程序崩溃问题
AutoPtr(AutoPtr<T>& ap)
:_ptr(ap._ptr)
{
ap._ptr = nullptr;
}
AutoPtr<T>& operator=(AutoPtr<T>& ap){
//检测是否给自己辅值
if(this != &ap){
//释放当前对象的资源
if(_ptr)
delete _ptr;
//转移ap资源到当前对象中
_ptr = ap._ptr;
ap._ptr = nullptr;
}
return *this;
}
T& operator*(){
return *_ptr;
}
T& operator->(){
return _ptr;
}
private:
T* _ptr;
};
class Date{
public:
Date(){
cout << "Date()" << endl;
}
~Date(){
cout << "~Date()" << endl;
}
public:
int _year;
int _month;
int _day;
};
int main(){
AutoPtr<Date> ap(new Date);
//拷贝后把ap对象赋空了,导致ap对象悬空
//通过ap对象访问资源时就会报错
AutoPtr<Date> copy(ap);
ap->_year = 2020;
return 0;
}
实现原理:简单粗暴的防拷贝,下面简化模拟实现一份UniquePtr来了解它的原理
template<class T>
class UniquePtr{
public:
UniquePtr(T* ptr = nullptr)
:_ptr(ptr)
{}
~UniquePtr(){
if(_ptr)
delete _ptr;
}
T& operator*(){
return *_ptr;
}
T& operator->(){
return _ptr;
}
private:
UniquePtr(UniquePtr<T> const &) = delete;
UniquePtr& operator=(UniquePtr<t> const &) = delete;
T* _ptr;
};
实现原理:通过引用计数的方式来实现多个shared_ptr对象之间的共享资源
模拟实现一份简单地SharedPtr,了解原理
template<class T>
class SharedPtr{
public:
SharedPtr(T* ptr = nullptr)
:_ptr(ptr)
,_pRefCount(new int(1))
,_pMutex(new mutex)
{}
~SharedPtr(){
Release();
}
SharedPtr(const SharedPtr<T>& sp)
:_ptr(sq._ptr)
,_pRefCount(sq._pRerCount)
,_pMutex(sq._pMutex)
{
AddRefCount();
}
//sp1 = sp2
SharedPtr<T>& operator=(const SharedPtr<T>& sp){
if(_ptr != sp._ptr){
//释放管理的就资源
Release();
//共享管理新对象的资源,增加引用计数
_ptr = sp._ptr;
_pRefCount = sp._pRefCount;
_pMutex = sp._pMutex;
AddRefCount();
}
return *this;
}
T& operator*(){
return *_ptr;
}
T& operator->(){
return _ptr;
}
int UseCount(){
retrun _pRefCount;
}
T* Get(){
return _ptr;
}
void AddRefCount(){
//加锁或者使用加一的原子操作
_pMutex->lock();
++_pRefCount;
_pMutex->unlock();
}
private:
void Release(){
bool deletefalg = false;
//引用计数减一,如果为0,则释放资源
_pMutex.lock();
if(--(*_pRefCount) == 0){
delete _ptr;
delete _pRefCount;
deletefalg = true;
}
_pMutex.unlock();
if(deletefalg == true)
delete _pMutex;
}
private:
T* _ptr; //指向管理资源的指针
int* _pRefCount; //引用计数
mutex* _pMutex; //互斥锁
};
通过下面的程序我们来测试shared_ptr的线程安全问题。需要注意的是shared_ptr的线程安全分为两方面:
struct ListNode
{
int _data;
shared_ptr<ListNode> _prev;
shared_ptr<ListNode> _next;
~ListNode(){ cout << "~ListNode()" << endl; }
};
int main(){
shared_ptr<ListNode> node1(new ListNode);
shared_ptr<ListNode> node2(new ListNode);
cout << node1.use_count() << endl;
cout << node2.use_count() << endl;
node1->_next = node2;
node2->_prev = node1;
cout << node1.use_count() << endl;
cout << node2.use_count() << endl;
return 0;
}
循环引用分析:
解决方案:在引用计数的场景下,把节点中的_prev和_next改成weak_ptr就可以了
本文地址:https://blog.csdn.net/weixin_43253845/article/details/107636750
如对本文有疑问, 点击进行留言回复!!
clion+vs编译器+Qt5中使用QPrinter和QprintDialog类
基于open62541在QT编写OPCUA特定的客户端程序(含有源码) + VS2015 C语言搭建OPCUA客户端环境
网友评论