摘 要:为了获得最佳性能,C/C++ 语言把操纵内存的权限以指针的方式暴露给开发人员。但是 C/C++ 语言编译器GCC 和 Clang 都不提供内存安全检测,导致开发人员使用 C/C++ 语言编写的项目可能存在内存泄漏的风险。文章介绍了改进指针算法和 shared_ptr 源码,分析了它们检测内存泄漏的方式,最后指出该类算法的缺陷,提出改进思路,并建议用户避免写出该类算法无法检测的代码结构。
关键词:C/C++ 程序;内存泄漏;改进指针算法;shared_ptr
DOI:10.19850/j.cnki.2096-4706.2021.22.029
基金项目:国家自然科学基金:内存安全 性和形式化规约的运行时验证(62172217)
中图分类号:TP311 文献标识码:A 文章编号:2096-4706(2021)22-0098-03
Research on Memory Leak Detection Algorithm of C/C++ Program
WU Jun
(College of Computer Science and Technology, Nanjing University of Aeronautics and Astronautics, Nanjing 211100, China)
Abstract: In order to obtain the best performance, C/C++ language exposes the permission to manipulate memory to developers in the form of pointers. However, the C/C++ language compiler GCC and Clang do not provide memory security detection, which leads to the risk of memory leakage for projects written by developers in C/C++ language. This paper introduces the improved pointer algorithm and shared_ptr source code, analyzes their ways to detect memory leakage, finally points out the defects of this kind of algorithm and puts forward improvement ideas, and advises users to avoid writing code structures that cannot be detected by such algorithms.
Keywords: C/C++ program; memory leak; improved pointer algorithm; shared_ ptr
参考文献:
[1] 朱云龙 .C 程序运行时监控和验证的插桩方法研究与应用 [D]. 南京:南京航空航天大学,2016.
[2] CHEN Z,TAO C Q,ZHANG Z Y,et al. Poster: Beyond Spatial and Temporal Memory Safety [C]//2018 IEEE/ACM 40th International Conference on Software Engineering: Companion (ICSECompanion). Gothenburg:IEEE,2018:189-190.
[3] 严峻琦 .C 程序内存安全错误的运行时检测技术研究与实现 [D]. 南京:南京航空航天大学,2017.
[4] 叶蓉,陈榕 . 运用 CAR 智能指针实现 Callback 机制 [J]. 计算机技术与发展,2008(2):9-12+16.
[5] 张彤,何源 . 一种自适应的引用计数智能指针的实现 [J]. 成都大学学报(自然科学版),2007(1):55-57.
作者简介:仵俊(1997—),男,汉族,江苏南京人,硕士研究生在读,研究方向:软件验证。