无论C还是C++中,我们从一开始接触他们,老时就开始强调在使用后要对内存进行释放,以免发生内存泄露,但是,我们总有粗心的时候,C++中提出了只能指针的概念,用只能指针类来管理所申请的内存,伴随着函数的结束,智能指针自动调用析构函数对其进行释放。

智能指针(auto_ptr)
*auto_ptr指针的所有权可以转移,可以在函数只见传递,但同一时刻只能有一个auto_*ptr管理指针,当auto_ptr对象获得该空间的所有权后,用它来拷贝构造新的对象时,它所管理的指针的所有权也会发生改变,然后将它内部的指针赋空:

简单代码实现:

template<class _Ty>
class auto_ptr
{
public:auto_ptr(_Ty *_P=0):_Ptr(_P){}auto_ptr(const auto_ptr<_Ty> &_Y):_Ptr(_Y.release()){}auto_ptr& operator=(auto_ptr& _a){if(this != &_a){reset(_a.release());}return *this;}~auto_ptr(){delete _Ptr;}
public:void reset(_Ty* _p = 0){if (_p != _Ptr){delete _Ptr;_Ptr = _p;}}_Ty& operator*()const{return (*get());}_Ty* operator->()const{return (get());}_Ty* get()const{return _Ptr;}_Ty* release()const{_Ty* tmp = _Ptr;((auto_ptr<_Ty>*)this)->_Ptr = 0;return tmp;}private:_Ty *_Ptr;
};int main()
{int *p = new int(10);auto_ptr<int> pa1(p);auto_ptr<int> pa2 = (pa1);auto_ptr<int> pa3;pa3 = pa2;//cout<<*pa2<<endl;//cout<<*pa1<<endl;return 0;
}

Scoped_ptr
scoped_ptr与auto_ptr十分相似,它时包装了new操作赋在对上分配的动态对象,能够保证动态创建的对象在任意时候都能被释放,相对于auto_ptr而言,scoped_ptr相对比较“自私”,它的所有权更见严格,一旦将所有权交给它,就无法再将所有权收回来,所以在scoped_ptr不允许进行赋值操作和拷贝构造。你可能会纠结它是如何实现的,就算你不给他赋值和拷贝构造函数,系统也会默认给出。其实很简单,将这两个函数声明为私有的就OK啦,下面我们一起来看下代码吧:

#include <iostream>
using namespace std;template<typename _Ty>
class scoped_ptr
{scoped_ptr(const scoped_ptr &_s){}scoped_ptr& operator=(scoped_ptr &_s){}
public:explicit scoped_ptr(_Ty *_p = 0):_Ptr(_p){}~scoped_ptr(){}
public:void reset(_Ty *_p = 0){if(_p != _Ptr){delete _Ptr;_Ptr = _p;}}_Ty& operator*()const{return *get();}_Ty* operator->()const{return (get());}_Ty* get()const{return _Ptr;}void swap(scoped_ptr &_s){_Ty *tmp = _s._Ptr;_s._Ptr = _Ptr;_Ptr = tmp;}
private:_Ty *_Ptr;
};int mian()
{int *p = new int(10);scoped_ptr<int> ps(p);cout << *ps << endl;return 0;
}

在类中成员默认为私有成员,所以这样就无法调动赋值操作和拷贝构造函数了。

scoped_array:
scoped_array主要是用来管理指针的,它的借口功能与scoped_ptr疾苦相同,scoped_array主要特点如下:
1、scoped_array在构造是接受的必须是new[]的结果;
2、scoped_array中没有“*” “->”操作赋的重载;
3、在析构时,用delete[]来释放内存;
4、重载[]运算符,使其能和普通数组一样用下标访问元素;
5、没有begin()、end()等类似容器的迭代器操作函数。

shared_ptr
shared_ptr与scoped_ptr相似,也是包装了new操作赋在堆上分配的动态对象,但它可以实现多个shared_ptr管理同一个指针,因为,它在实现时,在内便加入了使用计数器,只要拷贝一次引用计数器值加1,当引用计数器值不为一时部进行析构,这样防止了一块内存多次析构而导致的程序崩溃:

shared_ptr.h:

#ifndef _SHARED_PTR_H
#define _SHARED_PTR_H
//#include <iostream>
#include "shared_count.h"
#include "sp_counted_base.h"
//using namespace std;template<typename _Ty>
class shared_ptr
{
public:shared_ptr():px(0){cout << "Creat shared_ptr!!" << endl;}shared_ptr(const shared_ptr<_Ty> &r):px(r.px),pn(r.pn){cout << "Creat shared_ptr!!" << endl;}shared_ptr(_Ty *p):px(p),pn(p){cout << "Creat shared_ptr!!" << endl;}#if 1~shared_ptr(){cout << "    Free shared_ptr!!" << endl;if(use_count() == 0){cout << "Really free shared_ptr!!" << endl;cout << "haha use_count_ = "<<use_count()<<endl;px = 0;}}#endif
public:_Ty& operator*()const{return *px;}_Ty* operator->()const{return px;}
public:long use_count()const{return pn.use_count();}
public:void swap(shared_ptr<_Ty> &b){_Ty *temp;temp = px;px = b.px;b.px = temp;}
private:shared_count<_Ty> pn;_Ty *px;
};#endif

shared_count.h:

#ifndef _SHARED_COUNT_H
#define _SHARED_COUNT_H
#include <iostream>
#include "sp_counted_impl_xxx.h"
#include "sp_counted_base.h"
using namespace std;template<typename _Ty>
class shared_count
{
public:shared_count():pi_(0){cout << "Creat shared_counted!!" << endl;}shared_count(_Ty *p):pi_(new sp_counted_impl_xxx<_Ty>(p)){cout << "Creat shared_counted!!" << endl;}shared_count(const shared_count &r):pi_(r.pi_){pi_->add_ref_copy();}shared_count& operator=(const shared_count &r){pi_ = r.pi_;pi_->add_ref_copy();}~shared_count(){cout << "Free shared_counted!!" << endl;pi_->~sp_counted_base();if(use_count() == 0){pi = 0;}}
public:long use_count()const{return pi_->use_count();}void decrement(){pi_->release();if(use_count == 0){delete this;}}
private:sp_counted_base *pi_;
};#endif

sp_counted_base.h:

因为shared_count 里边的sp_counted_base *类型的pi_指向的是sp_counted_impl_xxx类里面的基类sp_counted_base,而基类无法调用子类的析构函数,而子类不析构基类也无法析构,所以,为了让他们能准确析构,在这里将sp_counted_base的析构函数定义为虚函数,在sp_counted_impl_xxx内重写。

#ifndef _SP_COUNTED_BASE_H
#define _SP_COUNTED_BASE_H
#include <iostream>
#include "sp_counted_impl_xxx.h"
using namespace std;//template<typename _Ty>
class sp_counted_base
{
public:sp_counted_base():use_count_(1){//use_count_ = 1;cout << "Creat sp_counted_base!!" << endl;}virtual ~sp_counted_base() //因为父类无法调用子类内部的函数,所以将其定义为虚函数,以保证它和它的子函数能够正常析构。{cout << "Free sp_counted_base" << endl;}
public:void add_ref_copy(){++ use_count_;}long use_count()const{return use_count_;}void release(){-- use_count_;}
private:long use_count_;
};#endif

sp_counted_impl_xxx.h:

#ifndef _SP_COUNTED_IMPL_XXX_H
#define _SP_COUNTED_IMPL_XXX_H
#include <iostream>
#include "sp_counted_base.h"
using namespace std;template<typename _Ty>
class sp_counted_impl_xxx : public sp_counted_base
{
public:sp_counted_impl_xxx(_Ty *p):px_(p){cout << "Creat sp_count_impl_xxx!!" << endl;}~sp_counted_impl_xxx(){while(use_count()){this->release();cout << "\n\nuse_count() = " << use_count() << endl;if(use_count() == 0){delete px_;cout << "Free sp_count_impl_xxx!!" << endl;}}}
private:_Ty *px_;
};#endif

main.cpp:

#include <iostream>
#include "shared_ptr.h"
#include "shared_count.h"
#include "sp_counted_base.h"
#include "sp_counted_impl_xxx.h"
using namespace std;int main()
{int *p = new int(10);shared_ptr<int>pa(p);cout << "pa = " << *pa << endl;cout << "pa use_count = " << pa.use_count() << endl;shared_ptr<int> ps1 = pa;cout << "ps use_count = " << pa.use_count() << endl;shared_ptr<int> ps2;ps2 = ps1;cout << "ps use_count = " << pa.use_count() << endl;return 0;
}

boost库:智能指针(auto_ptr、scoped_ptr、shared_ptr)浅谈相关推荐

  1. 模拟实现智能指针auto_ptr,scoped_ptr,shared_ptr

    智能指针,顾名思义它是一个聪明的指针,那么到底聪明到哪了呢,让我们一起来看以下的代码. void test1() //内存泄露 {int *p = new int(1);if (1){//...ret ...

  2. C++——智能指针——auto_ptr、shared_ptr、unique_ptr

    1.4.智能指针 智能指针是行为类似于指针的类对象. C++11 中提供了三种智能指针,使用这些智能指针时需要引用头文件 : ·shared_ptr; ·unique_ptr; ·auto_ptr; ...

  3. C++ boost库智能指针(四):intrusive_ptr

    intrusive_ptr这个智能指针不怎么好用,虽然它和shared_ptr也是基于引用计数的,但需要自己来实现引用计数的增加与减少.需要在我们管理的指针对象中添加一个计数的成员,例如下面的类Tes ...

  4. C++ -- 智能指针 auto_ptr,unique_ptr,shared_ptr的简单实现和原理

    一,为什么需要智能指针 智能指针是一种预防型的内存泄漏的解决方案.由于C++没有垃圾回收器机制,所以每次new出来的资源都需要手动的delete,如果没有手动释放,就会造成资源泄漏等问题.因此,为了避 ...

  5. 智能指针的用法shared_ptr

    目录 智能指针 shared_ptr共享的智能指针 shared_ptr的基本用法 1.初始化 reset成员函数 make_shared swap()函数 2. 获取原始指针(get()) 3. 指 ...

  6. C++11中的智能指针unique_ptr、shared_ptr和weak_ptr详解

    目录 1.引言 2.什么是智能指针? 3.在Visual Studio中查看智能指针的源码实现 4.独占式指针unique_ptr 4.1.查看unique_ptr的源码实现片段 4.2.为什么uni ...

  7. C++的智能指针auto_ptr、unique_ptr源码解析

    C++的智能指针auto_ptr.unique_ptr源码解析 1.前言 2.源码准备 3.源码解析 3.1.auto_ptr解析 3.2.unique_ptr解 3.3.unique_ptr的一个偏 ...

  8. c++系列 —— 智能指针auto_ptr和unique_ptr

    往期地址: c++系列一 -- c++的封装 c++系列二 -- c++的继承 c++系列三 -- 继承和多态特性 c++系列四 -- 运算符重载 c++系列五 -- 静态成员和静态类 c++系列六 ...

  9. 智能循迹小车_校园智能车障碍断路方案浅谈

    点击上方蓝字 关注小黑记事本 小黑提要: 华软嵌协举办的第十届校园智能车比赛即将开赛,组委会今年在赛道元素上进行了一次大创新,加入了没有出现过的障碍与断路新元素,对于参赛的选手是一个不小的挑战,而选手 ...

  10. 智能指针——auto_ptr

    1. 开篇 C++里面的四个智能指针:auto_ptr.unique_ptr.shared_ptr.weak_ptr,其中后三个是C++11支持,而这个auto_ptr已经被C++11弃用.但auto ...

最新文章

  1. stackoverflow国内被墙的打开办法
  2. halcon知识:图像纹理特征提取cooc_feature_matrix
  3. 【项目实战课】人人免费可学!基于Pytorch的图像分类简单任务数据增强实战
  4. 深入浅出设计模式——组合模式(Composite Pattern)
  5. 发现以前的一些记录 shutdown AP CPU
  6. 查询varchar实际大小_微服务和VUE(11): mybatis 动态查询
  7. python函数与方法的区别_python中函数和方法有哪些区别
  8. python接口自动化参数化_python接口自动化6-参数化关联
  9. WebRTC-集成qsv硬解码实现
  10. [转载] 【Python进阶】4-2 多态 | 什么是多态 / 静态语言vs动态语言 / python中多态
  11. 安装11.2.0.3时,OUI的log报错:OUI-10066:Not All The Dependencies For The Component ... Could Be Found
  12. python常用的开发环境包括_Python 全栈:Python 四种常用开发环境总结
  13. 从字节中取出1bit数据
  14. ADV7441驱动EDID配置及声音问题
  15. 数据抓取 | 数据分享 - 北京链家二手房成交数据抓取,保存格式为excel
  16. 深度学习入门(一)快速建立自己的图片数据集
  17. wordpress制作微信小程序源码
  18. 金蝶中间件AAS无法访问管理平台提示404
  19. 老是抓不住伦敦黄金实时行情,怎么办?
  20. 【信息系统项目管理师】2019年上半年信息系统项目管理师上午综合知识真题

热门文章

  1. 2021年4月29日
  2. 布道微服务_20微服务、容器、DevOps
  3. Ubuntu常用操作命令
  4. Python判断字符串相等
  5. 零基础学Java,有必要上培训班吗?
  6. SAP PA获利分析案例教程后台配置
  7. 树的计数 Prüfer编码与Cayley公式 学习笔记
  8. 一份标准的测试计划包含哪些要素?
  9. <数据结构>时间复杂度及空间复杂度
  10. html 填满父容器,CSS让子元素div的高度填满父容器的剩余空间的方法