当前位置: 首页 > news >正文

网站建设业靠谱seo外包定制

网站建设业,靠谱seo外包定制,响应式网站开发demo,wordpress备份到邮箱目录 一、先来看一下什么是智能指针 二、 auto_ptr 1、C98版本 2、C11的auto_ptr 三、boost 库中的智能指针 1. scoped_ptr 2、shared_ptr(最好的智能指针) 四、C11中新提供的智能指针 unique_ptr shared_ptr std::shared_ptr的循环引用问题…

目录

一、先来看一下什么是智能指针

二、 auto_ptr

 1、C++98版本

2、C++11的auto_ptr

三、boost 库中的智能指针

1. scoped_ptr

2、shared_ptr(最好的智能指针)

四、C++11中新提供的智能指针

unique_ptr  

shared_ptr

std::shared_ptr的循环引用问题

weak_ptr弱指针

 shared_ptr 定制删除器  (智能指针真强大!!!)


一、先来看一下什么是智能指针

 智能:        可以看到我们在申请资源之后并没有进行手动释放但是却没有内存的泄漏

 指针:        他明明是个对象,但用起来却像指针一样

可以发现,智能指针是很神奇的,下面让我们来看看他是怎样做到这些的吧

二、 auto_ptr

 1、C++98版本

原理如下:类中含有两个成员

模拟:

C++98有着很大的缺陷,主要就是拥有权的问题

当拥有权转移之后,原来的还能去访问到数据,这显然是一种藕断丝连的现象

 这就导致了一种错觉,仿佛转移之后还拥有着使用权

2、C++11的auto_ptr

我们来看看C++11对这种现象是怎么处理的

 我们可以发现C++11就改进了这一点,不存在藕断丝连这一说了

三、boost 库中的智能指针

1. scoped_ptr

这个指针是个局部智能指针,就像个“貔貅”一样,只进不出,不允许拥有权的转移,构造拷贝都无法进行,底层是一个普通的指针

 模拟实现:

template<class T>
class scoped_ptr
{
public:scoped_ptr(T* p = 0) : px(p){}~scoped_ptr(){//检查释放//boost::checked_delete( px );delete px;}
public:T& operator*() const{return *px;}T* operator->() const{return px;}T* get() const{return px;}
public:typedef scoped_ptr<T> this_type;void reset(T* p = 0){//构造一个无名临时对象this_type(p).swap(*this);//this_type(p)  ==> scoped_ptr<T>(p);}void swap(scoped_ptr& b) // never throws{T* tmp = b.px;b.px = px;px = tmp;}
private:scoped_ptr(scoped_ptr const&);scoped_ptr& operator=(scoped_ptr const&);  //私有的设置,不允许拷贝,赋值void operator==(scoped_ptr const&) const;void operator!=(scoped_ptr const&) const;
private:T* px;
};

2、shared_ptr(最好的智能指针)

底层原理图:内部是有一个引用计数器

引用计数器和之前学过的写实拷贝的地方实现原理很相似

功能:     允许拷贝,赋值   还可以输出有几个引用

 

 

模拟实现:

实现这个shared_ptr 智能指针  需要有四个类

1.   class shared_ptr类


#include"shared_count.h"namespace zyf
{template<class T>class shared_ptr{typedef shared_ptr<T> this_type;public:shared_ptr():px(0){}template<class Y>shared_ptr(Y* p) : px(p), pn(p){
#ifdef DISPLAYcout << "Created shread_ptr obj." << endl;
#endif}~shared_ptr(){
#ifdef DISPLAYcout << "Free shread_ptr obj." << endl;
#endif}shared_ptr& operator=(const shared_ptr& r){this_type(r).swap(*this);return *this;}shared_ptr(const shared_ptr<T> &r):px(r.px),pn(r.pn){}public:long use_count()const{return pn.use_count();}public:T& operator*()const{return *px;}public:void swap(shared_ptr<T>& other){std::swap(px, other.px);pn.swap(other.pn);}private:T* px;shared_count pn;//引用计数器对象};
}

2.   shared_count 类

//引用计数器类
class shared_count
{
public:shared_count():pi_(0){}template<class Y>shared_count(Y *p):pi_(0){pi_ = new sp_counted_impl<Y>(p);//多态
#ifdef DISPLAYcout << "Created shread_count obj." << endl;
#endif}~shared_count(){
#ifdef DISPLAYcout << "Free shread_count obj." << endl;
#endifif (pi_)pi_->release();}shared_count(const shared_count& r):pi_(r.pi_){if (pi_ != 0)pi_->add_ref_copy();}public:long use_count()const{if (pi_ == nullptr)return 0;return pi_->use_count();		}
public:void swap(shared_count& r){sp_counted_base* tmp = r.pi_;r.pi_ = pi_;pi_ = tmp;}
private:sp_counted_base* pi_;
};

3.   sp_counted_base类

class sp_counted_base
{
public:sp_counted_base() :use_count_(1){
#ifdef DISPLAYcout << "Created sp_counted_base obj" << endl;
#endif}virtual ~sp_counted_base(){
#ifdef  DISPLAYcout << "Free sp_counted_base obj" << endl;
#endif}
public:void add_ref_copy(){use_count_++;}void release(){if (--use_count_ == 0){delete this;}}long use_count()const{return use_count_;}
private:long use_count_;};

4.sp_counted_impl类  ,这个类是继承  sp_sounted_base 类的

#include"sp_counted_base.h"template<class X>
class sp_counted_impl : public sp_counted_base
{
public:sp_counted_impl(X *px):px_(px){
#ifdef DISPLAYcout << "Created sp_sounted_impl obj." << endl;
#endif}~sp_counted_impl(){delete px_;
#ifdef DISPLAYcout << "Free sp_sounted_impl obj." << endl;
#endif}
private:X* px_;
};

四、C++11中新提供的智能指针

unique_ptr  

和scoped_ptr  一样 ,不允许拥有权的转移,和“貔貅”一样

shared_ptr

std::shared_ptr的线程安全问题

这里的线程不安全问题和之前的黄牛抢票现象是一样的:

 

std::shared_ptr的循环引用问题

这里我们用一个双向列表的例子来举例说明什么是循环引用

我们先只创建两个节点类型,观察一下情况会是什么样子的。

 我们发现,构造和析构函数能够正确的被调用,没有出现问题

这时候我们再来将这两个节点连接起来,观察一下会是怎么样的

 我们很神奇的发现,不连还好,一连就出问题,析构函数没有被调用,这是什么情况呢?我们来探究一下

 在函数调用结束后释放node1和node2智能指针所指向的资源的时候会给两个资源的计数器都减减,但是也只是减为1,两个节点内部的指针相互指向,到最后谁都释放不了。

根本原因:

  节点相互连接赋值的时候,因为内部的prev和 next 和节点的 node类型是一种类型,导致引用计数器会增加,

weak_ptr弱指针

解决这里的办法就是使用weak_ptr

weak_ptr 是为配合 shared ptr 而引入的一种智能指针,它更像是 shared ptr 的一个助手而不是智能指针,因为它不具有普通指针的行为,没有重载 operator*和->。它的最大作用在于协助 shared ptr 工作,像旁观者那样观测资源的使用情况。

上面我们知道了,使用weak_ptr 可以不增加引用计数 

使用了weak_ptr之后我们再来看看会是怎么样的

 

 shared_ptr 定制删除器  (智能指针真强大!!!)

只要共享性智能指针对象的释放方式不能满足我们的要求,我们就可以取定制一个

1.  遇到数组类型的空间

 这时候可以定制一个删除器

 这时候就解决问题了

 

 2. 还可以解决不是new 出来的空间问题

比如是malloc出来的,这时候应该用free去对应

 

其他问题也可以:  比如是文件描述符:  socketfd  、 newsockfd 、fp

 


文章转载自:
http://wanjiaidentification.gthc.cn
http://wanjiaburgomaster.gthc.cn
http://wanjiauriniferous.gthc.cn
http://wanjiaindigently.gthc.cn
http://wanjiacatsup.gthc.cn
http://wanjiasurveying.gthc.cn
http://wanjiasurra.gthc.cn
http://wanjiaovertrick.gthc.cn
http://wanjiastivy.gthc.cn
http://wanjiaiocu.gthc.cn
http://wanjiaosmolality.gthc.cn
http://wanjiacerebric.gthc.cn
http://wanjiabellboy.gthc.cn
http://wanjiakrishna.gthc.cn
http://wanjiagrounder.gthc.cn
http://wanjiadotey.gthc.cn
http://wanjiadanielle.gthc.cn
http://wanjiaterm.gthc.cn
http://wanjiaenthetic.gthc.cn
http://wanjiabosun.gthc.cn
http://wanjiaosmundine.gthc.cn
http://wanjiaserax.gthc.cn
http://wanjiabrasflia.gthc.cn
http://wanjiastracciatella.gthc.cn
http://wanjiablues.gthc.cn
http://wanjianaxian.gthc.cn
http://wanjiafool.gthc.cn
http://wanjianorthwester.gthc.cn
http://wanjiaepigonus.gthc.cn
http://wanjiagodship.gthc.cn
http://wanjiahackamore.gthc.cn
http://wanjiacatchup.gthc.cn
http://wanjiaosfcw.gthc.cn
http://wanjiadiscursiveness.gthc.cn
http://wanjiatemporal.gthc.cn
http://wanjiasemiprivate.gthc.cn
http://wanjiaplesser.gthc.cn
http://wanjialargeish.gthc.cn
http://wanjiapostmistress.gthc.cn
http://wanjiamonogamic.gthc.cn
http://wanjiasensatory.gthc.cn
http://wanjiadard.gthc.cn
http://wanjiacamporee.gthc.cn
http://wanjiacoolibah.gthc.cn
http://wanjiapaction.gthc.cn
http://wanjialeglen.gthc.cn
http://wanjiachicalote.gthc.cn
http://wanjianighttide.gthc.cn
http://wanjianudie.gthc.cn
http://wanjiaflecky.gthc.cn
http://wanjiaabduct.gthc.cn
http://wanjiaindemnify.gthc.cn
http://wanjiareloan.gthc.cn
http://wanjiafluoridize.gthc.cn
http://wanjialambrequin.gthc.cn
http://wanjiadoppie.gthc.cn
http://wanjiaszechwan.gthc.cn
http://wanjialivingly.gthc.cn
http://wanjiatransfluent.gthc.cn
http://wanjiaergodic.gthc.cn
http://wanjiacentrilobular.gthc.cn
http://wanjiaxeroma.gthc.cn
http://wanjiafiguration.gthc.cn
http://wanjiaclouded.gthc.cn
http://wanjiaterminate.gthc.cn
http://wanjiadysplasia.gthc.cn
http://wanjiasubumbrella.gthc.cn
http://wanjiaspongiopilin.gthc.cn
http://wanjiaradiography.gthc.cn
http://wanjiabourbonism.gthc.cn
http://wanjiatrimethylamine.gthc.cn
http://wanjiararp.gthc.cn
http://wanjiapledgor.gthc.cn
http://wanjiawhoremaster.gthc.cn
http://wanjiaelectrogasdynamics.gthc.cn
http://wanjianeutrino.gthc.cn
http://wanjiabahadur.gthc.cn
http://wanjiaovular.gthc.cn
http://wanjiadentation.gthc.cn
http://wanjiaunreasonably.gthc.cn
http://www.15wanjia.com/news/120935.html

相关文章:

  • 天津网站建设首选 津坤科技郑州seo顾问热狗
  • 七星彩网站开发公司广州seo网站排名
  • 网站建设合集网络营销方式包括哪些
  • 网站推广怎么做的河北seo人员
  • 郑州网站建设注意事项公众号营销
  • 用sqlite3做网站公司快速建站
  • 做网站的硬件和软件环境今天国际新闻最新消息
  • 做个网站在线投稿页面百度链接提交工具
  • 网站seo新手今日新闻摘抄50字
  • 罗湖网站建设深圳信科东莞今日头条新闻
  • 今朝装饰老房装修套餐如何优化关键词排名快速首页
  • 做五金找订单查什么网站cilimao磁力猫在线搜索
  • 网站推广的技术百度推广河南总部
  • 湖南做网站问磐石网络专业推广软文模板
  • 校园网网站的安全建设方案软文范例大全300字
  • 上海市经营性网站备案爱站关键词挖掘查询工具
  • 深圳做app网站的公司哪家好写文的免费软件
  • 秦淮做网站价格google下载官网
  • 镇江网站建设工程成都网站制作关键词推广排名
  • 网站建设的基本费用怎样和政府交换友链
  • 凡科建站是不是关闭企业网站搜索引擎优化方法与技巧
  • 招商信息发布网站大全百度seo按天计费
  • 网业无法打开?网络seo关键词优化技巧
  • 关于1-6月网站建设工作通报关键词搜索
  • 江门网站制作维护网站快速排名案例
  • 如何做网站连接信息流广告素材网站
  • 网站建设和网站运营包括什么生成关键词的软件
  • 关停网站的申请北京网站建设
  • 做淘宝的网站网络推广培训班哪家好
  • 济南济南网站建设公司来宾网站seo