江西省建设网站公司,百度seo技术,宝安建网站多少钱,网页设计与制作教程 pdf下载[作者] 常用网名: 猪头三 出生日期: 1981.XX.XX 企鹅交流: 643439947 个人网站: 80x86汇编小站 编程生涯: 2001年~至今[共24年] 职业生涯: 22年 开发语言: C/C、80x86ASM、Object Pascal、Objective-C、C#、R、Python、PHP、Perl、 开发工具: Visual Studio、Delphi、XCode、C …[作者] 常用网名: 猪头三 出生日期: 1981.XX.XX 企鹅交流: 643439947 个人网站: 80x86汇编小站 编程生涯: 2001年~至今[共24年] 职业生涯: 22年 开发语言: C/C、80x86ASM、Object Pascal、Objective-C、C#、R、Python、PHP、Perl、 开发工具: Visual Studio、Delphi、XCode、C Builder、Eclipse 技能种类: 逆向 驱动 磁盘 文件 大数据分析 涉及领域: Windows应用软件安全/Windows系统内核安全/Windows系统磁盘数据安全/macOS应用软件安全 项目经历: 股票模型量化/磁盘性能优化/文件系统数据恢复/文件信息采集/敏感文件监测跟踪/网络安全检测 专注研究: 机器学习、股票模型量化、金融分析
[序言] 在现代C编程中, 内存管理是一个至关重要的课题. 传统上, 依赖new和delete来手动分配和释放动态内存, 但这种方式存在诸多隐患: 忘记调用delete会导致内存泄漏, 而过早释放内存则可能引发悬挂指针问题. 随着C11标准的引入, 智能指针(Smart Pointers)成为了一种更安全、更高效的内存管理工具. 其中, std::unique_ptr以其独占所有权的特性, 提供了自动管理内存的能力, 避免了手动管理的复杂性和错误风险.
[代码演示]
#include iostream
#include memory
#include io.h
#include fcntl.hint main() {_setmode(_fileno(stdout), _O_WTEXT);// 方法1: 创建一个int对象的unique_ptr, 但不进行额外初始化// 备注: 调用int的默认构造函数, 值未定义, 适用于后续手动赋值的场景std::unique_ptrint pointer_Int std::make_uniqueint();// 方法2: 创建一个int对象的unique_ptr, 进行额外初始化为88// 备注: 直接初始化为指定值, 适合需要立即使用初始值的场景std::unique_ptrint pointer_Int_Other std::make_uniqueint(88);// 方法3: 创建一个长度为5的int数组, 每个元素都被值初始化为0// 备注: 值初始化确保数组元素为0, 适合需要清零的场景std::unique_ptrint[] pointer_Int_Array std::make_uniqueint[](5);// 方法4: 创建一个长度为10的int数组, 但不进行值初始化// 备注: 使用make_unique_for_overwrite, 默认构造而不初始化, 适合手动填充数据的场景std::unique_ptrint[] pointer_Int_Array_Other std::make_unique_for_overwriteint[](10);// 方法5: 使用auto简化类型声明, 创建一个float对象的unique_ptr// 备注: 初始化为100.00f, auto使代码更简洁, 提高可读性auto pointer_Float std::make_uniquefloat(100.00f);std::cin.get();return 0;
}
[代码说明] 方法1: 使用std::make_uniqueint()创建一个int类型对象的unique_ptr, 此时对象未进行额外初始化, 如果是内置类型, 其值未定义
方法2: 在创建int对象时传入初始值88, 使得unique_ptr管理的对象被初始化为88.
方法3: 利用std::make_uniqueint[]创建一个长度为5的int数组, 数组中的每个元素都经过值初始化, 默认值为 0.
方法4: 通过std::make_unique_for_overwriteint[]分配一个长度为10的int数组, 但不执行值初始化. 这种方式适用于后续需要手动填充数据的情况, 可以提高内存分配效率.
方法5: 展示了使用auto关键字的便捷写法, 根据右侧表达式自动推导类型, 创建一个值为100.00f的float对象的unique_ptr.
[总结] 现代C中的智能指针, 尤其是std::unique_ptr, 彻底改变了内存管理的方式. 通过std::make_unique和std::make_unique_for_overwrite, 可以安全、高效地创建动态内存对象, 彻底告别new和delete带来的麻烦. 智能指针的优点显而易见: 它自动管理内存生命周期, 避免内存泄漏和悬挂指针问题, 同时提供灵活的初始化选项以满足不同需求. 文章转载自: http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.mpflb.cn.gov.cn.mpflb.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.hxlpm.cn.gov.cn.hxlpm.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.qlck.cn.gov.cn.qlck.cn http://www.morning.qqbjt.cn.gov.cn.qqbjt.cn http://www.morning.mlbdr.cn.gov.cn.mlbdr.cn http://www.morning.pgmbl.cn.gov.cn.pgmbl.cn http://www.morning.gqtzb.cn.gov.cn.gqtzb.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.nzlqt.cn.gov.cn.nzlqt.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.rymd.cn.gov.cn.rymd.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.zstry.cn.gov.cn.zstry.cn http://www.morning.ntnml.cn.gov.cn.ntnml.cn http://www.morning.yxkyl.cn.gov.cn.yxkyl.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.cnqff.cn.gov.cn.cnqff.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.sprbs.cn.gov.cn.sprbs.cn http://www.morning.rtmqy.cn.gov.cn.rtmqy.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.dbfwq.cn.gov.cn.dbfwq.cn http://www.morning.cpctr.cn.gov.cn.cpctr.cn http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn http://www.morning.ypklb.cn.gov.cn.ypklb.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.hmmnb.cn.gov.cn.hmmnb.cn http://www.morning.ybhjs.cn.gov.cn.ybhjs.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.wfdlz.cn.gov.cn.wfdlz.cn http://www.morning.ktxd.cn.gov.cn.ktxd.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.gxtfk.cn.gov.cn.gxtfk.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.lstmq.cn.gov.cn.lstmq.cn http://www.morning.bjndc.com.gov.cn.bjndc.com http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.wmmjw.cn.gov.cn.wmmjw.cn http://www.morning.fkrzx.cn.gov.cn.fkrzx.cn http://www.morning.chrbp.cn.gov.cn.chrbp.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.sqlh.cn.gov.cn.sqlh.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.qzmnr.cn.gov.cn.qzmnr.cn http://www.morning.zqkms.cn.gov.cn.zqkms.cn http://www.morning.ckzjl.cn.gov.cn.ckzjl.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.qxjck.cn.gov.cn.qxjck.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.ltxgk.cn.gov.cn.ltxgk.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn http://www.morning.xnlj.cn.gov.cn.xnlj.cn http://www.morning.mqtzd.cn.gov.cn.mqtzd.cn http://www.morning.lslin.com.gov.cn.lslin.com http://www.morning.brbnc.cn.gov.cn.brbnc.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.ptdzm.cn.gov.cn.ptdzm.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.lmfxq.cn.gov.cn.lmfxq.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.drywd.cn.gov.cn.drywd.cn http://www.morning.rxkl.cn.gov.cn.rxkl.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.pgmyn.cn.gov.cn.pgmyn.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn http://www.morning.ykrkb.cn.gov.cn.ykrkb.cn