代码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> v1; v2(v1); 容器的拷贝构造和容器的赋值容易引起容器元素的拷贝构造和赋值,而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 = 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<typename T>
class MySmartPtr2 { public: //新创建的智能指针,默认计数器为1
MySmartPtr2<T> (T * mptr=nullptr): _mptr(mptr){
_pRef = new RefCount<T>(_mptr,1);
} //拷贝构造
MySmartPtr2<T>(const MySmartPtr2<T> & _rval) {
//两个智能指针指向相同的资源 this->_mptr = _rval._mptr;
this->_pRef = _rval._pRef;
this->_pRef->addCount();
}
//赋值重载
MySmartPtr2<T> & operator=(const MySmartPtr2<T> & _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<T>() { 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<T> * _pRef;
}; int main() {
MySmartPtr2<int> ms1(new int(100)) ; {
MySmartPtr2<int> 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 }

上面代码造成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 和 weak_ptr 是线程安全的.

最新文章

  1. iOS 使用EZAudio库生成wav出错的情况
  2. 【Android Studio快捷键】之代码提示
  3. jquery.tmpl.js 模板引擎用法
  4. django处理静态文件
  5. SSH Secure Shell Client中文乱码的解决办法
  6. hdu3033 I love sneakers! 分组背包变形
  7. UMDH
  8. Android的Fragment中onActivityResult不被调用的解决方案
  9. 用Python设计第一个游戏 - 零基础入门学习Python002
  10. java异常的一些小知识
  11. Java WEB工程搭建UEditor
  12. js中的break,continue和return到底怎么用?
  13. 利用ajax获取网页表单数据,并存储到数据库之二(使用SSH)
  14. 跟我学ASP.NET MVC之二:第一个ASP.NET MVC程序
  15. Python读取文件时输入文件绝对路径报错
  16. 平时作业五 Java
  17. 第75节:Java的中的JSP,EL和JSTL
  18. Spark机器学习(11):协同过滤算法
  19. [Golang] 从零開始写Socket Server(4):将执行參数放入配置文件(XML/YAML)
  20. ASP.Net 中操作Ajax

热门文章

  1. .NET Core Web APi类库如何内嵌运行?
  2. 记录一个奇葩的问题:k8s集群中master节点上部署一个单节点的nacos,导致master节点状态不在线
  3. 延申三大问题中的第二个问题处理---收集查看k8s中pod的控制台日志
  4. 线程池底层原理详解与源码分析(补充部分---ScheduledThreadPoolExecutor类分析)
  5. [题解] Topcoder 15279 SRM 761 Div 1 Level 3 SpanningSubgraphs DP,容斥
  6. [CG从零开始] 3. 安装 pyassimp 库加载模型文件
  7. 分布式存储系统之Ceph集群访问接口启用
  8. 关于Jenkins-Item-Office 365 Connector-下的多选框的参数定义
  9. windows C++ call ADB command
  10. 微信电脑版DAT文件转图片工具