Cppcheck是一种C/C++代码缺陷静态检查工具,不同于C/C++编译器及其它分析工具,Cppcheck只检查编译器检查不出来的bug,不检查语法错误。执行的检查包括:
 
1 #include <stdio.h> 2 3 int main() 4 { 5 int *a = new int[3]; 6 printf("%d\n", a[4]); 7 return 0; 8 }
使用命令如下
cppcheck -q --enable=performance --platform=unix32 --language=c++ test.cpp
结果如下
Checking test.cpp ... [test.cpp:6]: (error) Array ‘a[3]‘ accessed at index 4, which is out of bounds. [test.cpp:7]: (error) Memory leak: a
分别指出内存越界、以及内存泄漏的问题。
原文:https://www.cnblogs.com/NoSoul/p/13356866.html