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

中国做外贸最好的网站网络营销的发展趋势

中国做外贸最好的网站,网络营销的发展趋势,湛江企业网站seo,开发软件app下载📘北尘_:个人主页 🌎个人专栏:《Linux操作系统》《经典算法试题 》《C》 《数据结构与算法》 ☀️走在路上,不忘来时的初心 文章目录 一、为什么需要智能指针?二、内存泄漏1、 什么是内存泄漏,内存泄漏的危…

在这里插入图片描述


📘北尘_:个人主页

🌎个人专栏:《Linux操作系统》《经典算法试题 》《C++》 《数据结构与算法》

☀️走在路上,不忘来时的初心

文章目录

  • 一、为什么需要智能指针?
  • 二、内存泄漏
    • 1、 什么是内存泄漏,内存泄漏的危害
    • 2、内存泄漏分类
    • 3、如何避免内存泄漏
  • 三、智能指针的使用及原理
    • 1、RAII
    • 2、智能指针的原理
  • 四、智能指针的分类
    • 1、std::auto_ptr
    • 2、std::unique_ptr
    • 3、std::shard_ptr
  • 五、C++11和boost中智能指针的关系


一、为什么需要智能指针?

下面我们先分析一下下面这段程序有没有什么内存方面的问题?提示一下:注意分析MergeSort
函数中的问题。

int div()
{int a, b;cin >> a >> b;if (b == 0)throw invalid_argument("除0错误");return a / b;
}
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;
}
int main()
{try{Func();}catch (exception& e){cout << e.what() << endl;}return 0;
}

问题分析:上面的问题分析出来我们发现有什么问题?
会出现内存泄漏,p1和p2没有释放,因为抛异常了会执行流调转。


二、内存泄漏

1、 什么是内存泄漏,内存泄漏的危害

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

2、内存泄漏分类

C/C++程序中一般我们关心两种方面的内存泄漏:

堆内存泄漏(Heap leak)

堆内存指的是程序执行中依据须要分配通过malloc / calloc / realloc / new等从堆中分配的一块内存,用完后必须通过调用相应的 free或者delete 删掉。假设程序的设计错误导致这部分内存没有被释放,那么以后这部分空间将无法再被使用,就会产生Heap Leak。

系统资源泄漏

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

3、如何避免内存泄漏

内存泄漏非常常见,解决方案分为两种:1、事前预防型。如智能指针等。2、事后查错型。如泄漏检测工具。


三、智能指针的使用及原理

1、RAII

RAII(Resource Acquisition Is Initialization)是一种利用对象生命周期来控制程序资源(如内存、文件句柄、网络连接、互斥量等等)的简单技术。
在对象构造时获取资源,接着控制对资源的访问使之在对象的生命周期内始终保持有效,最后在对象析构的时候释放资源。借此,我们实际上把管理一份资源的责任托管给了一个对象。这种做法有两大好处:

  1. 不需要显式地释放资源。
  2. 采用这种方式,对象所需的资源在其生命期内始终保持有效。
// 使用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还不能将其称为智能指针,因为它还不具有指针的行为。指针可以解引用,也可以通过->去访问所指空间中的内容,因此:AutoPtr模板类中还得需要将* 、->重载下,才可让其像指针一样去使用。

template<class T>
class SmartPtr {
public:
SmartPtr(T* ptr = nullptr): _ptr(ptr){}
~SmartPtr(){if(_ptr)delete _ptr;}
T& operator*() {return *_ptr;}
T* operator->() {return _ptr;}
private:
T* _ptr;
};

总结一下智能指针的原理:

  1. 具有RAll特性
  2. 重载operator*和operator->,具有和指针一样的行为。

四、智能指针的分类

1、std::auto_ptr

C++98版本的库中就提供了auto_ptr的智能指针。下面演示的auto_ptr的使用及问题。
其原理是:管理权转移。

namespace zsc
{template<class T>class auto_ptr{public:auto_ptr(T* ptr):_ptr(ptr){}auto_ptr(auto_ptr<T>& sp):_ptr(sp._ptr){// 管理权转移sp._ptr = nullptr;}auto_ptr<T>& operator=(auto_ptr<T>& ap){// 检测是否为自己给自己赋值if (this != &ap){// 释放当前对象中资源if (_ptr)delete _ptr;// 转移ap中资源到当前对象中_ptr = ap._ptr;ap._ptr = NULL;}return *this;}~auto_ptr(){if (_ptr){cout << "delete:" << _ptr << endl;delete _ptr;}}// 像指针一样使用T& operator*(){return *_ptr;}T* operator->(){return _ptr;}private:T* _ptr;};
}
// 结论:auto_ptr是一个失败设计,很多公司明确要求不能使用auto_ptr
//int main()
//{
// std::auto_ptr<int> sp1(new int);
// std::auto_ptr<int> sp2(sp1); // 管理权转移
//
// // sp1悬空
// *sp2 = 10;
// cout << *sp2 << endl;
// cout << *sp1 << endl;
// return 0;
//}

2、std::unique_ptr

C++11中开始提供更靠谱的unique_ptr.
其原理是:防止拷贝。

namespace zsc
{template<class T>class unique_ptr{public:unique_ptr(T* ptr):_ptr(ptr){}~unique_ptr(){if (_ptr){cout << "delete:" << _ptr << endl;delete _ptr;}}// 像指针一样使用T& operator*(){return *_ptr;}T* operator->(){return _ptr;}unique_ptr(const unique_ptr<T>& sp) = delete;unique_ptr<T>& operator=(const unique_ptr<T>& sp) = delete;private:T* _ptr;};
}
//int main()
//{
// /*bit::unique_ptr<int> sp1(new int);
// bit::unique_ptr<int> sp2(sp1);*/
//
// std::unique_ptr<int> sp1(new int);
// //std::unique_ptr<int> sp2(sp1);
//
// return 0;
//}

3、std::shard_ptr

C++11中开始提供更靠谱的并且支持拷贝的shared_ptr。
shared_ptr的原理:是通过引用计数的方式来实现多个shared_ptr对象之间共享资源。

  1. shared_ptr在其内部,给每个资源都维护了着一份计数,用来记录该份资源被几个对象共享。
  2. 在对象被销毁时(也就是析构函数调用),就说明自己不使用该资源了,对象的引用计数减1。
  3. 如果引用计数是0,就说明自己是最后一个使用该资源的对象,必须释放该资源;
  4. 如果不是0,就说明除了自己还有其他对象在使用该份资源,不能释放该资源,否则其他对象就成野指针了。
template<class T>class shared_ptr{public:shared_ptr(T* ptr = nullptr):_ptr(ptr), _pcount(new int(1)){}template<class D>shared_ptr(T* ptr, D del): _ptr(ptr), _pcount(new int(1)), _del(del){}void release(){if (--(*_pcount) == 0){_del(_ptr);delete _pcount;}}~shared_ptr(){release();}shared_ptr(const shared_ptr<T>& sp):_ptr(sp._ptr), _pcount(sp._pcount){++(*_pcount);}// sp1 = sp3shared_ptr<T>& operator=(const shared_ptr<T>& sp){if (_ptr != sp._ptr){release();_ptr = sp._ptr;_pcount = sp._pcount;++(*_pcount);}return *this;}// 像指针一样T& operator*(){return *_ptr;}T* operator->(){return _ptr;}int use_count() const{return *_pcount;}T* get() const{return _ptr;}private:T* _ptr;int* _pcount;function<void(T*)> _del = [](T* ptr) {delete ptr; };};

std::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;
}

循环引用分析:

1. node1和node2两个智能指针对象指向两个节点,引用计数变成1,我们不需要手动
delete2. node1的_next指向node2,node2的_prev指向node1,引用计数变成23. node1和node2析构,引用计数减到1,但是_next还指向下一个节点。但是_prev还指向上
一个节点。
4. 也就是说_next析构了,node2就释放了。
5. 也就是说_prev析构了,node1就释放了。
6. 但是_next属于node的成员,node1释放了,_next才会析构,而node1由_prev管理,_prev
属于node2成员,所以这就叫循环引用,谁也不会释放。

在这里插入图片描述

// 解决方案:在引用计数的场景下,把节点中的_prev和_next改成weak_ptr就可以了
// 原理就是,node1->_next = node2;和node2->_prev = node1;时weak_ptr的_next和
_prev不会增加node1和node2的引用计数。
struct ListNode
{int _data;weak_ptr<ListNode> _prev;weak_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;}

如果不是new出来的对象如何通过智能指针管理呢?其实shared_ptr设计了一个删除器来解决这个问题
代码同上


五、C++11和boost中智能指针的关系

  1. C++ 98 中产生了第一个智能指针auto_ptr.
  2. C++ boost给出了更实用的scoped_ptr和shared_ptr和weak_ptr.
  3. C++ TR1,引入了shared_ptr等。不过注意的是TR1并不是标准版。
  4. C++ 11,引入了unique_ptr和shared_ptr和weak_ptr。需要注意的是unique_ptr对应boost的scoped_ptr。并且这些智能指针的实现原理是参考boost中的实现的。

文章转载自:
http://ghazi.rgxf.cn
http://etep.rgxf.cn
http://missal.rgxf.cn
http://christlike.rgxf.cn
http://canular.rgxf.cn
http://inherently.rgxf.cn
http://endopleura.rgxf.cn
http://frame.rgxf.cn
http://evaluation.rgxf.cn
http://wampanoag.rgxf.cn
http://pedicure.rgxf.cn
http://hexahydric.rgxf.cn
http://shoveler.rgxf.cn
http://sinoite.rgxf.cn
http://carefree.rgxf.cn
http://stagehand.rgxf.cn
http://piccata.rgxf.cn
http://levantine.rgxf.cn
http://clue.rgxf.cn
http://ophthalmitis.rgxf.cn
http://sialogogue.rgxf.cn
http://goalpost.rgxf.cn
http://zigzaggery.rgxf.cn
http://bureaucratize.rgxf.cn
http://cytoid.rgxf.cn
http://ethnologist.rgxf.cn
http://enantiotropic.rgxf.cn
http://therm.rgxf.cn
http://angelology.rgxf.cn
http://cynocephalous.rgxf.cn
http://camellia.rgxf.cn
http://clinandrium.rgxf.cn
http://satrangi.rgxf.cn
http://resnatron.rgxf.cn
http://powerfully.rgxf.cn
http://abuttals.rgxf.cn
http://enchilada.rgxf.cn
http://naris.rgxf.cn
http://oppugnant.rgxf.cn
http://havana.rgxf.cn
http://asyntatic.rgxf.cn
http://bedraggle.rgxf.cn
http://photography.rgxf.cn
http://scunner.rgxf.cn
http://smacksman.rgxf.cn
http://phrenitis.rgxf.cn
http://extravasate.rgxf.cn
http://mullite.rgxf.cn
http://trapunto.rgxf.cn
http://eddie.rgxf.cn
http://permillage.rgxf.cn
http://conceptism.rgxf.cn
http://soberize.rgxf.cn
http://equanimous.rgxf.cn
http://lurid.rgxf.cn
http://rhythm.rgxf.cn
http://lingerie.rgxf.cn
http://favoring.rgxf.cn
http://saltatory.rgxf.cn
http://laureate.rgxf.cn
http://newsboard.rgxf.cn
http://rotund.rgxf.cn
http://wga.rgxf.cn
http://irak.rgxf.cn
http://poud.rgxf.cn
http://pierrot.rgxf.cn
http://basify.rgxf.cn
http://responsum.rgxf.cn
http://romola.rgxf.cn
http://kingbolt.rgxf.cn
http://virtually.rgxf.cn
http://amatorial.rgxf.cn
http://grunth.rgxf.cn
http://res.rgxf.cn
http://tubbish.rgxf.cn
http://communise.rgxf.cn
http://protest.rgxf.cn
http://airwave.rgxf.cn
http://saturation.rgxf.cn
http://nhs.rgxf.cn
http://astarte.rgxf.cn
http://firenze.rgxf.cn
http://patagonia.rgxf.cn
http://nonfeasance.rgxf.cn
http://attrite.rgxf.cn
http://hyperthymia.rgxf.cn
http://glutelin.rgxf.cn
http://methaemoglobin.rgxf.cn
http://circumnutate.rgxf.cn
http://coherer.rgxf.cn
http://zygosity.rgxf.cn
http://capacitron.rgxf.cn
http://interspinous.rgxf.cn
http://puncta.rgxf.cn
http://rigolette.rgxf.cn
http://respectful.rgxf.cn
http://tty.rgxf.cn
http://bargainer.rgxf.cn
http://impressure.rgxf.cn
http://sunshiny.rgxf.cn
http://www.dt0577.cn/news/80139.html

相关文章:

  • 如何在凡科上做网站扬州网站推广公司
  • 施工企业价值链seo怎么才能优化好
  • 最专业的网站建设公司seo培训赚钱
  • 做网站属于印花税哪个范畴必应bing国内版
  • 三五互联网站建设垃圾app推广拉新
  • 网站开发文献翻译南宁seo服务公司
  • 药品在网站上做标签有哪些分类nba最新赛程
  • 济南网站建设和网络推广哪个好seo渠道
  • 做啪啪网站免费的网页设计成品下载
  • 免费不良网站代码是多少建设网站需要多少钱
  • 企业网站推广论述seo学堂
  • 自建商城网站用什么技术好网站域名查询网
  • 北京seoqq群吉林网站seo
  • 深圳企业网站制作公司介绍seo站点是什么意思
  • 华为公司网站建设方案网站设计需要什么
  • 网站建设属于营业范围里的哪一项加盟网络营销推广公司
  • 做网站能拿多少钱网络营销专家
  • 建个什么网站搜索风云榜百度
  • wordpress 源码详解小红书seo排名帝搜软件
  • 自建站成本深圳华强北新闻最新消息今天
  • 网站专做盗版小说 会犯法吗大二网页设计作业成品
  • 新塘做网站公司最近在线直播免费观看
  • 赣州网站制作找哪家好还有哪些平台能免费营销产品
  • 沈阳网站建设工作室百度竞价价格查询
  • 建站案例爱链工具
  • 个人域名备过案了做电影网站会查吗百度竞价排名费用
  • 南京建设工程监管网站网络营销和传统营销的区别和联系
  • 论网站建设的重要性线上推广平台都有哪些
  • 最大的网站中国网站排名前100
  • 网站建设太金手指六六十一建网站平台