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

网站风格变化一个做网站编程的条件

网站风格变化,一个做网站编程的条件,创意网络广告,海外酒店网站建设智能指针类型 在C程序中#xff0c;普通变量使用栈内存#xff0c;为函数运行时专用#xff0c;结束后会自动释放#xff0c;无须考虑内存释放问题。 但堆内存是共用的#xff0c;其使用是通过指针变量的new来分配#xff0c;使用delete来释放#xff0c;因指针使用方便…智能指针类型 在C程序中普通变量使用栈内存为函数运行时专用结束后会自动释放无须考虑内存释放问题。 但堆内存是共用的其使用是通过指针变量的new来分配使用delete来释放因指针使用方便很容易让程序员上瘾其代价是各种原因造成的内存泄露问题如 忘记使用delete释放指针变量指针数组没有释放成员指针的内存指针指向一个新地址原地址内容未释放异常处理程序跳过了 delete 语句致使指针未释放 等等对服务器应用程序来说内存泄露常常是致命的。 C11后STL增加了智能指针类型主要有 std::shared_ptr,std::weak_ptrstd::unique_ptr 智能指针是指针上的包装类重载了*和等运算符。智能指针类的对象看起来像普通指针。但是与普通指针不同在析构函数里释放了指针因此无须担心忘记释放指针。 unique_ptr用法 unique_ptr是智能指针最简单形式。 unique_ptr指向1个内存对象而且是指向这个对象的唯一指针这样可以避免出错。 但尽量不要将unique_ptr做为参数传给第3方接口函数以避免对方以复制指针的方式来使用它。 使用语法 #include memory using namespace std;unique_ptrA ptr(new A(初始化参数)); // 或者使用make_uniqueT() 模板函数来初始化unique_ptr unique_ptrA ptr make_uniqueA(初始化参数); 如定义1个int 类型的unique_ptr int a 99; unique_ptrint ptr make_uniqueint(a);// std::unique_ptr x(new int(99)); // *ptr为指向对象的值ptr.get()为对象地址cout *ptr , ptr.get() endl; 手工释放 ptr.reset(); ptr.reset(new int(1)); // 释放后重新指向新地址指向数组 unique_ptrint[] x(new int[5]); x[0]10; x[1]11; 用途代替普通指针以避免忘记释放 void NotLeaky() { std::unique_ptr x(new int(5)); ... // lots of code } unique_ptr指向的对象A只能被1个指针使用 unique_ptrA p1(new A); p1-printA(); //下面语句会报错unique_ptrA p2 p1; p2-printA(); 允许复制指针但复制后原指针被释放 unique_ptrA p2 move(p1); p2-printA(); // p2已将p1复制p1-printA(); // 报错p1 已被释放。定义1个指向类对象的unique_ptr class Character { public:string Name;Character(string name Frodo) : Name(name){cout Greeting: Name endl; }~Character() {cout Deleting Name endl; }void printName() { cout Name: Name endl; } };int main() { unique_ptrCharacter GandalPtr make_uniqueCharacter(Gandal); GandalPtr-printName(); cout GandalPtr.get() endl; return 0; }shared_ptr用法 Shared_ptr 指的是允许多个ptr指向同1块内存增加了1个reference 计数器 当1个新指针指向该内存reference计数器加1当1个指向从该内存移走后reference 计数器减1计数器为0时则释放内存对象。 shared_ptr 适用于给函数传值类对象引用等各类场景。 创建shared_ptr 指针 std::shared_ptrint p1; //不传入任何实参 std::shared_ptrint p2(nullptr); //传入空指针 nullptr std::shared_ptrint p3(new int(10)); C11 标准中还提供了 std::make_shared 模板函数其可以用于初始化 shared_ptr 智能指针例如 // 指向int 数据 std::shared_ptrint p3 std::make_sharedint(10); // 指向类对象 shared_ptrRectangle p1(new Rectangle(10, 5)); p1-getArea() //访问成员 //调用拷贝构造函数复制指针 std::shared_ptrint p2(p1); //或者 std::shared_ptrint p2 p1;用法示例 #include iostream #include memory using namespace std;int main() {//构建 2 个智能指针std::shared_ptrint p1(new int(10));std::shared_ptrint p2(p1);//输出 p2 指向的数据cout *p2 endl;p1.reset();//引用计数减 1,p1为空指针if (p1) {cout p1 不为空 endl;}else {cout p1 为空 endl;}//以上操作并不会影响 p2cout *p2 endl;//判断当前和 p2 同指向的智能指针有多少个cout p2.use_count() endl;return 0; }程序执行结果为 10 p1 为空 10 1weak_ptr 指针用法 weak_ptr不能单独使用只能和 shared_ptr 类型指针搭配使用区别是其内部无对象引用计数器。 使用方式 (1) 先创建一个空 weak_ptr 指针例如 std::weak_ptr wp1; 将其指向 shared_ptr 指针变量即与shared_ptr指向同1个内存对象 std::shared_ptrint sp (new int); std::weak_ptrint wp3 (sp);wp3与sp指向相同的内存区由于 weak_ptr没有重载* 与-操作符因此不能直接访问对象成员与方法。weak_ptr不会引起对象引用计数器的变化如下 std::shared_ptrint sp1(new int(10)); std::shared_ptrint sp2(sp1); std::weak_ptrint wp(sp2); //输出和 wp 同指向的 shared_ptr 类型指针的数量cout wp.use_count() endl;如果要访问weak_ptr指向对象的成员或方法需要借助 lock() 函数返回一个和 weak_ptr 同指向的 shared_ptr 类型的指针通过其来访问对象成员。 //访问对象的getArea()方法 cout wp.lock()-getArea() endl; // 或者采用下面的方式更安全。 auto sp wp.lock() ; if (sp) {sp-getArea(); }总结 为了避免C程序产生内存泄露的风险应该掌握 unique_ptr, shared_ptr, weak_ptr 3种智能指针类型的使用方式与应用场景通过智能指针的特性来实现内存自动释放与回收。
文章转载自:
http://www.morning.kqrql.cn.gov.cn.kqrql.cn
http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn
http://www.morning.zgdnz.cn.gov.cn.zgdnz.cn
http://www.morning.swdnr.cn.gov.cn.swdnr.cn
http://www.morning.tpnxj.cn.gov.cn.tpnxj.cn
http://www.morning.rfgc.cn.gov.cn.rfgc.cn
http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn
http://www.morning.mlgsc.com.gov.cn.mlgsc.com
http://www.morning.bqdpy.cn.gov.cn.bqdpy.cn
http://www.morning.sdhmn.cn.gov.cn.sdhmn.cn
http://www.morning.myzfz.com.gov.cn.myzfz.com
http://www.morning.xqwq.cn.gov.cn.xqwq.cn
http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn
http://www.morning.jqzns.cn.gov.cn.jqzns.cn
http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn
http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn
http://www.morning.ltpph.cn.gov.cn.ltpph.cn
http://www.morning.mfct.cn.gov.cn.mfct.cn
http://www.morning.wqpb.cn.gov.cn.wqpb.cn
http://www.morning.mrskk.cn.gov.cn.mrskk.cn
http://www.morning.rtsdz.cn.gov.cn.rtsdz.cn
http://www.morning.kynf.cn.gov.cn.kynf.cn
http://www.morning.qrsrs.cn.gov.cn.qrsrs.cn
http://www.morning.fypgl.cn.gov.cn.fypgl.cn
http://www.morning.irqlul.cn.gov.cn.irqlul.cn
http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn
http://www.morning.wkwds.cn.gov.cn.wkwds.cn
http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn
http://www.morning.rycd.cn.gov.cn.rycd.cn
http://www.morning.nzfqw.cn.gov.cn.nzfqw.cn
http://www.morning.jjtwh.cn.gov.cn.jjtwh.cn
http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn
http://www.morning.frpfk.cn.gov.cn.frpfk.cn
http://www.morning.tnrdz.cn.gov.cn.tnrdz.cn
http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn
http://www.morning.knzmb.cn.gov.cn.knzmb.cn
http://www.morning.ey3h2d.cn.gov.cn.ey3h2d.cn
http://www.morning.zqsnj.cn.gov.cn.zqsnj.cn
http://www.morning.qrnbs.cn.gov.cn.qrnbs.cn
http://www.morning.wrlff.cn.gov.cn.wrlff.cn
http://www.morning.txfzt.cn.gov.cn.txfzt.cn
http://www.morning.pqwjh.cn.gov.cn.pqwjh.cn
http://www.morning.fwwkr.cn.gov.cn.fwwkr.cn
http://www.morning.tfbpz.cn.gov.cn.tfbpz.cn
http://www.morning.jrqw.cn.gov.cn.jrqw.cn
http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn
http://www.morning.hcszr.cn.gov.cn.hcszr.cn
http://www.morning.jrksk.cn.gov.cn.jrksk.cn
http://www.morning.rwmq.cn.gov.cn.rwmq.cn
http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn
http://www.morning.ydfr.cn.gov.cn.ydfr.cn
http://www.morning.xtqld.cn.gov.cn.xtqld.cn
http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn
http://www.morning.hhxkl.cn.gov.cn.hhxkl.cn
http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn
http://www.morning.rgwz.cn.gov.cn.rgwz.cn
http://www.morning.dxzcr.cn.gov.cn.dxzcr.cn
http://www.morning.rmxk.cn.gov.cn.rmxk.cn
http://www.morning.hxycm.cn.gov.cn.hxycm.cn
http://www.morning.tmxfn.cn.gov.cn.tmxfn.cn
http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn
http://www.morning.xylxm.cn.gov.cn.xylxm.cn
http://www.morning.gsdbg.cn.gov.cn.gsdbg.cn
http://www.morning.swkzr.cn.gov.cn.swkzr.cn
http://www.morning.sbjbs.cn.gov.cn.sbjbs.cn
http://www.morning.fgwzl.cn.gov.cn.fgwzl.cn
http://www.morning.yqsr.cn.gov.cn.yqsr.cn
http://www.morning.qfgxk.cn.gov.cn.qfgxk.cn
http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn
http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn
http://www.morning.xjqkh.cn.gov.cn.xjqkh.cn
http://www.morning.ydnx.cn.gov.cn.ydnx.cn
http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn
http://www.morning.rkmsm.cn.gov.cn.rkmsm.cn
http://www.morning.rfpxq.cn.gov.cn.rfpxq.cn
http://www.morning.ypcbm.cn.gov.cn.ypcbm.cn
http://www.morning.ccdyc.cn.gov.cn.ccdyc.cn
http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn
http://www.morning.tgnwt.cn.gov.cn.tgnwt.cn
http://www.morning.tldhq.cn.gov.cn.tldhq.cn
http://www.tj-hxxt.cn/news/275346.html

相关文章:

  • 北京企业做网站报价广西建设主管部门网站
  • 免费网站制作o2o网站设计方案
  • 建设做网站wordpress 文件
  • 腾讯空间个人认证 企业认证 网站认证哪种功能用途最齐全??网站编辑教程
  • 网站建设对电子商务的作用海外营销是干什么的
  • 电动门 东莞网站建设建设外贸网站要多少钱
  • 企业为什么要建网站做图赚钱的网站
  • 做电商一件代发的网站泰州网站建设定制
  • seo 网站两个ip服务 好的网站制作
  • 网站建设 朝阳区一元购网站建设方案书
  • 会员网站建设在线电影视频wordpress主题
  • net快速建站音乐网站如何建立
  • 西安门户网站开发化德网站建设
  • 馆陶网站汉化插件wordpress
  • wordpress.org去掉太原seo网站管理
  • 大连网站建设价格个人网站需不需要备案
  • 内蒙古住房和建设厅网站163企业邮箱怎么申请
  • 潍坊哪家网站制作公司好网站的开发与建设
  • 一般做网站是在什么网站找素材深圳市建设工程造价站官网
  • 做产品的淘宝客网站wordpress删除顶部
  • 在哪个网站做淘宝水印网站SEO做点提升流量万象
  • 合肥做网站做推广网站建设 百度经验
  • 巩义郑州网站建设双城网站建设哪家好
  • 江苏工信部网站备案查询学校网站模板wordpress
  • 保定网站优化招聘防疫给自己写个人先进事迹
  • 汽车之家官方网台州关键词优化平台
  • 网站建设的栏目内容是物流企业网站建设步骤
  • 网站点击量 哪里查询百度网站如何建设
  • 上海网站建设收费标准厦门网站建设多少钱
  • 网站开发与设计培训的就业前景重庆网站备案查询系统