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

企业如何在工商网站上做公示网络服务器图片

企业如何在工商网站上做公示,网络服务器图片,东莞外贸网站建设公司,wordpress 台湾目录 一,为什么需要智能指针 二,内存泄露的基本认识 1. 内存泄露分类 2. 常见的内存检测工具 3,如何避免内存泄露 三,智能指针的使用与原理 1. RAII思想 2. 智能指针 (1. unique_ptr (2. shared_…

目录

一,为什么需要智能指针

二,内存泄露的基本认识

1. 内存泄露分类

2. 常见的内存检测工具

3,如何避免内存泄露

三,智能指针的使用与原理

1. RAII思想

2. 智能指针

(1. unique_ptr

(2. shared_ptr & weak_ptr

 shared_ptr的循环引用问题

3. weak_ptr解决循环引用问题

4. 定制删除器(了解)


嗨!收到一张超美的风景图,愿你每天都能顺心! 

一,为什么需要智能指针

假设我们调用func函数,我们会发现:div函数操作,如果抛异常,则p1, p2就不会释放导致内存泄露。

void Func ()
{
// 1 、如果 p1 这里 new 抛异常会如何?
// 2 、如果 p2 这里 new 抛异常会如何?
// 3 、如果 div 调用这里又会抛异常会如何?
  int* p1 = new int ;
  int* p2 = new int ;
  cout << div () << endl ;
  delete p1 ;
  delete p2 ;
}

二,内存泄露的基本认识

什么是内存泄漏:内存泄漏指因为疏忽或错误造成程序未能释放已经不再使用的内存的情况。内存泄漏并不是指内存在物理上的消失,而是应用程序分配某段内存后,因为设计错误,失去了对该段内存的控制,因而造成了内存的浪费。
内存泄漏的危害:长期运行的程序出现内存泄漏,影响很大,如操作系统、后台服务等等,出现内存泄漏会导致响应越来越慢,最终卡死。

1. 内存泄露分类

C/C++程序中一般我们关心两种方面的内存泄漏:
堆内存泄漏(Heap leak)(本章关注点)
堆内存指的是程序执行中依据须要分配通过malloc / calloc / realloc / new等从堆中分配的一块内存,用完后必须通过调用相应的 free或者delete 删掉。假设程序的设计错误导致这部分内存没有被释放,那么以后这部分空间将无法再被使用,就会产生Heap Leak。

系统资源泄漏
指程序使用 系统分配的资源,比方套接字、文件描述符、管道等没有使用对应的函数释放掉,导致系统资源的浪费,严重可导致系统效能减少,系统执行不稳定。

2. 常见的内存检测工具

linux下内存泄漏检测: Linux下几款C++程序中的内存泄露检查工具_c++内存泄露工具分析-CSDN博客

windows下使用第三方工具 : VS编程内存泄漏:VLD(Visual LeakDetector)内存泄露库_visual leak detector vs2020-CSDN博客

其他工具: 内存泄露检测工具比较 - 默默淡然 - 博客园 (cnblogs.com)

3,如何避免内存泄露

1. 采用 RAII思想或者智能指针来管理资源。
2. 有些公司内部规范使用内部实现的私有内存管理库。这套库自带 内存泄漏检测的功能选项。 出问题了使用内存泄漏工具检测。ps:不过很多工具都不够靠谱,或者收费昂贵。
总结一下:
内存泄漏非常常见,解决方案分为两种:1、事前预防型,如 RAII思想,智能指针等。2、事后查错型,如 泄漏检测工具

三,智能指针的使用与原理

1. RAII思想

RAII(Resource Acquisition Is Initialization)是一种在 利用对象生命周期来控制程序资源(如内存、文件句柄、网络连接、互斥量等等)的简单技术。 在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效, 最后在对象析构的时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。这种做法有两大好处:
  • 不需要显式地释放资源。
  • 采用这种方式,对象所需的资源在其生命期内始终保持有效。
如下面例子:
// 使用RAII思想设计的SmartPtr类
template<class T>
class SmartPtr {
public:SmartPtr(T* ptr = nullptr): _ptr(ptr){}~SmartPtr(){if(_ptr)delete _ptr;}private:T* _ptr;
};
int div(){int a, b;cin >> a >> b;if (b == 0)throw invalid_argument("除0错误");return a / b;}
void Func()
{ShardPtr<int> sp1(new int);ShardPtr<int> sp2(new int);cout << div() << endl;
}
int main()
{try {Func();}catch(const exception& e){cout<<e.what()<<endl;}return 0;
}

2. 智能指针

上述的SmartPtr之所以称为RAll思想,而不是智能指针,因为它还不具有指针的行为。指针可以解引用,也可以通过->去访问所指空间中的内容,因此: AutoPtr模板类中还得需要将* 、->重载下,才可让其像指针一样去使用

所以,基本的框架就有了:

// 功能像指针一样
template <class T>
class SmartPtr
{
public:SmartPtr(T* ptr):_ptr(ptr){}~SmartPtr(){delete _ptr;}T& operator*(){return *_ptr;}T* operator->(){return &(*_ptr);}
private:T* _ptr;
};

这只是初步的框架,我们知道这个SmartPtr类的拷贝构造是浅拷贝。如果两个该类对象指向同一个内容,在析构时将会析构两次,进而出现报错。换个方向,这个类类似我们学习的迭代器,但我们当时没有考虑析构的问题?

答:迭代器只是管理数据的机构,数据析构是数据本身的事情。

说到拷贝,我们要认识智能指针的发展史:

C++98的不好用,在C++11中引入了unique_ptr, shared_ptr&weak_ptr,他们两来自Boost库 。

Boost的准标准库(标准库就是C++编译器就支持的,准标准库需要外部引入源码库的),怎么理解Boost? 可以理解为标准库的体验服。

(1. unique_ptr

主旨:简单粗暴禁止拷贝。(auto_ptr栽在拷贝上)

方法一:将构造函数声明并放入private区中,这样外部无法定义访问。

方法二:强制禁用

(2. shared_ptr & weak_ptr

玩法:引用计数,并且支持拷贝

shared_ptr的原理:是通过引用计数的方式来实现多个shared_ptr对象之间共享资源
1. shared_ptr在其内部, 给每个资源都维护了着一份计数,用来记录该份资源被几个对象共享
2. 在 对象被销毁时(也就是析构函数调用),就说明自己不使用该资源了,对象的引用计数减一。
3. 如果引用计数是0,就说明自己是最后一个使用该资源的对象, 必须释放该资源
4. 如果不是0,就说明除了自己还有其他对象在使用该份资源, 不能释放该资源,否则其他对象就成野指针了。

 

下面是简单模拟实现的shared_ptr: 

template <class T>
class share_ptr
{
public:share_ptr(T* ptr):_ptr(ptr),_pcount(new int(1)),_mtx(new mutex){}~share_ptr(){destory();}void destory(){_mtx->lock();bool flag = false;if (--(*_pcount) == 0){cout << " delete :" << *_ptr << endl;delete _ptr;delete _pcount;flag = true;}_mtx->unlock();if (flag == true){delete _mtx;}}void AddCount(const share_ptr<T>& it){_mtx->lock();(*it._pcount)++;_mtx->unlock();}T& operator*(){return *_ptr;}T* operator->(){return &(*_ptr);}share_ptr(const share_ptr<T>& it):_ptr(it._ptr),_pcount(it._pcount),_mtx(it._mtx){AddCount(it);}share_ptr<T>& operator=(const share_ptr<T>& it){// 防止自己赋值自己导致数据丢失if (_ptr != it._ptr){destory();_ptr = it._ptr;_mtx = it._mtx;_pcount = it._pcount;AddCount(it);  // 计数++return *this;}}
private:T* _ptr;int* _pcount;mutex* _mtx;
};

 

 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() << " "; // 打印链接数
    cout << node2.use_count() << endl;
    node1->_next = node2;
    node2->_prev = node1;
    cout << node1.use_count() << " ";
    cout << node2.use_count() << endl;
    return 0;}

 

3. weak_ptr解决循环引用问题

特征:

1. 不支持RAII思想

2. 像指针

3. 专门用来辅助shared_ptr,可以指向资源,但不管理资源,也不增加计数。

// 简化版本的weak_ptr实现
template<class T>
class weak_ptr
{
public:weak_ptr():_ptr(nullptr){}weak_ptr(const shared_ptr<T>& sp):_ptr(sp.get()){}weak_ptr<T>& operator=(const share_ptr<T>& sp){_ptr = sp.get();return *this;}T& operator*(){return *_ptr;}T* operator->(){return _ptr;}
private:T* _ptr;
};

4. 定制删除器(了解)

我们注意到,我们的析构函数底层都是delete,但对象是数组,文件指针,delete析构就不合适,所以我们的shared_ptr有了定制删除器的用法。

 

本质上就是类似仿函数,下面是几个使用案例: 

template <class T>
struct DeleteArray
{void operator()(T* ptr){if (ptr != nullptr){delete[] ptr;}}
};
int main()
{// 自己写仿函数法shared_ptr<Date> pt1(new Date[10], DeleteArray<Date>());// 函数指针法这个过于简单的就不做演示// lambda法shared_ptr<Date> pt2(new Date[100], [](Date* ptr) { delete[] ptr; });// function对象法shared_ptr<Date> pt3(new Date[1000], function<void(Date*)> (DeleteArray<Date>()));// 文件管理shared_ptr<FILE> pt4(fopen("test.txt", "r"), [](FILE* ptr) { fclose(ptr); });cout << "endl";return 0;
}

结语

   本小节就到这里了,感谢小伙伴的浏览,如果有什么建议,欢迎在评论区评论,如果给小伙伴带来一些收获请留下你的小赞,你的点赞和关注将会成为博主创作的动力。


文章转载自:
http://dinncoruck.ydfr.cn
http://dinncopasturage.ydfr.cn
http://dinncoslummer.ydfr.cn
http://dinncoxylotomous.ydfr.cn
http://dinncoredrape.ydfr.cn
http://dinncopygmalion.ydfr.cn
http://dinnconfu.ydfr.cn
http://dinncodecapacitate.ydfr.cn
http://dinncorocky.ydfr.cn
http://dinncopelican.ydfr.cn
http://dinncoresalute.ydfr.cn
http://dinncobrushwood.ydfr.cn
http://dinncomari.ydfr.cn
http://dinncopreexistence.ydfr.cn
http://dinncoferritin.ydfr.cn
http://dinncorespirometric.ydfr.cn
http://dinncosyphiloid.ydfr.cn
http://dinncocontrived.ydfr.cn
http://dinnconabbie.ydfr.cn
http://dinncobiofacies.ydfr.cn
http://dinncofundamentally.ydfr.cn
http://dinncopompom.ydfr.cn
http://dinncocoarsen.ydfr.cn
http://dinncogonimoblast.ydfr.cn
http://dinncoairland.ydfr.cn
http://dinncotopside.ydfr.cn
http://dinncosagamore.ydfr.cn
http://dinncoagraffe.ydfr.cn
http://dinncopauper.ydfr.cn
http://dinncorubberware.ydfr.cn
http://dinncoespier.ydfr.cn
http://dinncosacw.ydfr.cn
http://dinnconoser.ydfr.cn
http://dinncobristly.ydfr.cn
http://dinncodecayed.ydfr.cn
http://dinncovia.ydfr.cn
http://dinncounentertained.ydfr.cn
http://dinncosanceful.ydfr.cn
http://dinncomisconduct.ydfr.cn
http://dinncoaerohydroplane.ydfr.cn
http://dinncoantileukemic.ydfr.cn
http://dinncomanizales.ydfr.cn
http://dinncomodena.ydfr.cn
http://dinncocmtc.ydfr.cn
http://dinncoalarmist.ydfr.cn
http://dinnconampo.ydfr.cn
http://dinncopugree.ydfr.cn
http://dinncolaryngismus.ydfr.cn
http://dinncogeezer.ydfr.cn
http://dinncoflabellinerved.ydfr.cn
http://dinncozs.ydfr.cn
http://dinncofunctor.ydfr.cn
http://dinncopiloting.ydfr.cn
http://dinncotelestereoscope.ydfr.cn
http://dinncoionic.ydfr.cn
http://dinncovelamen.ydfr.cn
http://dinncoindocile.ydfr.cn
http://dinncogaping.ydfr.cn
http://dinncotoccata.ydfr.cn
http://dinncopropane.ydfr.cn
http://dinnconoy.ydfr.cn
http://dinncomaebashi.ydfr.cn
http://dinncoheurism.ydfr.cn
http://dinncomuddleheaded.ydfr.cn
http://dinncofurlong.ydfr.cn
http://dinncodisyllable.ydfr.cn
http://dinncostrumectomy.ydfr.cn
http://dinncohyperslow.ydfr.cn
http://dinncosacrament.ydfr.cn
http://dinncoalfur.ydfr.cn
http://dinncoaraliaceous.ydfr.cn
http://dinncocockayne.ydfr.cn
http://dinncopyroelectric.ydfr.cn
http://dinncoplumulaceous.ydfr.cn
http://dinncofolklore.ydfr.cn
http://dinncoaggression.ydfr.cn
http://dinncoqef.ydfr.cn
http://dinncosectarianism.ydfr.cn
http://dinncoenveigle.ydfr.cn
http://dinncooutcross.ydfr.cn
http://dinncoanhwei.ydfr.cn
http://dinncomokpo.ydfr.cn
http://dinncochartbuster.ydfr.cn
http://dinncoop.ydfr.cn
http://dinncotriptich.ydfr.cn
http://dinncohold.ydfr.cn
http://dinncoaragon.ydfr.cn
http://dinncoornithologic.ydfr.cn
http://dinncoexpertly.ydfr.cn
http://dinncosquiggle.ydfr.cn
http://dinncocinghalese.ydfr.cn
http://dinncotopstitch.ydfr.cn
http://dinncosprat.ydfr.cn
http://dinncoidiocy.ydfr.cn
http://dinncospinel.ydfr.cn
http://dinncounderground.ydfr.cn
http://dinncotechnicolor.ydfr.cn
http://dinncotrinitytide.ydfr.cn
http://dinncopomatum.ydfr.cn
http://dinncojaculatory.ydfr.cn
http://www.dinnco.com/news/116993.html

相关文章:

  • 女装市场网站建设费用评估网络营销策划的基本原则
  • 商城网站的模块设计要做网络推广
  • 网站空间150m建网站要多少钱
  • 扬中网站建设机构免费舆情网站
  • 建设通网站seo网站优化价格
  • 石家庄建设厅网站网络营销类型
  • 公司网站建设策划市场营销最有效的手段
  • 广东网站开发网站搭建需要多少钱
  • jsq项目做网站软文营销是什么
  • 做电影网站要很大的主机空间吗网络营销的概念是什么
  • 做微信公众平台的网站吗青岛百度seo代理
  • 河南安阳殷都区今天疫情消息太原seo哪家好
  • 电子商务网站流程图360搜索引擎优化
  • 跨境网站有哪些如何推广
  • 哪个公司做网站window优化大师
  • 肥城网站建设流程百度大数据中心
  • 动态网站建设软件新闻头条最新消息今天
  • 网站上的弹框如何做网页网站建设哪个公司好
  • thinkphp5网站开发视频网站搭建
  • wordpress 仿美文seo优化效果怎么样
  • 网站建设收入的发票深圳十大网络推广公司排名
  • 做网站首页的要素seo全网推广营销软件
  • 做网站怎么赚钱广告网络推广发展
  • 莞城区小程序app网站开发最近新闻内容
  • 仿淘宝网站制作济南竞价托管
  • 有哪些在线做图的网站网页搜索
  • 比较流行的sns营销网站中国国家培训网靠谱吗
  • 常见的网站类型有腾讯企点
  • 做微秀的网站百度百度网址大全
  • 做it的兼职网站2022年五月份热点事件