cppcheck是一个C/C++静态检查工具。它可以帮助我们检测出代码存在(潜在)的问题,比如数组越界、内存申请未释放、文件打开未关闭。注意,cppcheck不是编译器,替代不了gcc。

在ubuntu下安装cppcheck十分简单,命令如下:

root@latelee:latelee# apt-get install cppcheck

下面代码片段包含了数组越界、内存申请未释放、文件打开未关闭等错误。由于是演示用,不必太在意代码细节。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>void init_buffer(void)
{char filename[128] = {""}; // 这样初始为0,如果是{"1"},则只有第1个字节为1,其它为0 --不知其它编译器会怎样printf("test of buffer\n");dump(filename, 128);char unused_buffer[7*1024*1024] = {0};   // 没有使用的缓冲区,超过栈最大值,有coredump。char unused_buffer1[1*1024*1024] = {0};strcpy(unused_buffer1, "hello");
}// 数组范围越界
void out_of_array(void)
{int foo[2]; // 范围不够int aaa = 250;foo[0] = 1;foo[1] = 2;// 下面这些是不行的foo[2] = 3;foo[3] = 4;foo[4] = 5;foo[5] = 6;printf("%d %d \n", foo[0], foo[1]);
}#include <sys/types.h>
#include <dirent.h>
// 打开未关闭
void open_not_close()
{// 内存泄漏char* p = new char[100];strcpy(p, "hello");printf("p:%s\n", p);FILE* fp = NULL;fp = fopen("aaa", "a");if (fp){// 注:这里返回时没有关闭文件return;}fclose(fp);DIR *dir = NULL;dir = opendir("./");}int main(void)
{int ret = 0;foo();init_buffer();out_of_array();open_not_close()return 0;
}

注意,在open_not_close函数中的return前并没有关闭fp文件指针。这种错误特别容易发生,幸运的是,cppcheck可以检查出来。下面是是检测结果:

root@latelee~/test# cppcheck gcc_warning.cpp  --enable=all
Checking gcc_warning.cpp...
[gcc_warning.cpp:56] -> [gcc_warning.cpp:50]: (warning) Possible null pointer dereference: fp - otherwise it is redundant to check it against null.
[gcc_warning.cpp:13]: (style) Variable 'unused_buffer' is assigned a value that is never used.
[gcc_warning.cpp:23]: (style) Variable 'aaa' is assigned a value that is never used.
[gcc_warning.cpp:59]: (style) Variable 'dir' is assigned a value that is never used.
[gcc_warning.cpp:65]: (style) Variable 'ret' is assigned a value that is never used.
[gcc_warning.cpp:28]: (error) Array 'foo[2]' accessed at index 2, which is out of bounds.
[gcc_warning.cpp:29]: (error) Array 'foo[2]' accessed at index 3, which is out of bounds.
[gcc_warning.cpp:30]: (error) Array 'foo[2]' accessed at index 4, which is out of bounds.
[gcc_warning.cpp:31]: (error) Array 'foo[2]' accessed at index 5, which is out of bounds.
[gcc_warning.cpp:53]: (error) Memory leak: p
[gcc_warning.cpp:53]: (error) Resource leak: fp
[gcc_warning.cpp:61]: (error) Resource leak: dir
Checking usage of global functions..
(information) Cppcheck cannot find all the include files (use --check-config for details)

从上述信息中可以清晰地看到哪些代码存在问题,原因也一一给出。根据提示去修改代码即可。

注1:只要是人写的代码,都有可能存在这种那种问题。代码问题关键因素还是人,但可以借助工具帮助我们减少错误。——至少,如果在某个角落中数组越界了,cppcheck能检测出来。

注2:上述代码无法通过g++编译,因为有很多错误。但cppcheck不是编译器,所以无法检测出来。

注3:cppcheck不是万能的,比如在一个函数申请二级内存,在另一个函数不释放,此情况有内存泄漏,但cppcheck检测不出来。

另外也可以参考笔者的文章:

《gcc较高版本的一些编译警告收集》

《继续收集gcc一些编译警告》

《GCC编译警告选项的学习》

李迟 2016.6.15 周三 晚

使用cppcheck检测代码警告、错误相关推荐

  1. cppcheck卸载linux,使用cppcheck检测代码警告、错误

    cppcheck是一个C/C++静态检查工具.它可以帮助我们检测出代码存在(潜在)的问题,比如数组越界.内存申请未释放.文件打开未关闭.注意,cppcheck不是编译器,替代不了gcc. 在ubunt ...

  2. QT调试技巧-使用静态代码检测工具Cppcheck检测代码

    #QT调试技巧-使用静态代码检测工具Cppcheck检测代码 如需转载请标明出处:http://blog.csdn.net/itas109 QQ技术交流群:129518033 环境: Qt Creat ...

  3. make警告:检测到时钟错误。您的创建可能是不完整的

    前几天在带本科生毕设的时候,让他们的电脑是新装的ubuntu mate系统以及新装的ros kinetic版本,在更改了cpp文件后编译的过程中出现了问题:"警告:检测到时钟错误.您的创建可 ...

  4. 程序代码错误检测_错误检测代码

    程序代码错误检测 错误检测代码 (Error Detecting Codes) A group of bits is known as words, and these words move as a ...

  5. 【解决】make: 警告:检测到时钟错误。您的创建可能是不完整的

    make编译的时候,出现"make: 警告:检测到时钟错误.您的创建可能是不完整的"错误. 解决方法: touch * 发现make依然出时钟告警,由于目录下面还有文件夹,没有遍历 ...

  6. cppcheck静态代码检查工具使用教程

    0 背景 最近调研了几款 c/c++ 代码静态检查工具,包括 cppcheck.cpplint.cppdepend.splint.tscancode.sonaqube 等,对比后认为 cppcheck ...

  7. CppCheck静态代码检查配置(命令行方式或在VS中使用)

    目录 CppCheck静态代码检查 1.1 安装cppcheck 1.2 直接使用Cppcheck 1.2.1 命令行方式 1.2.2 UI方式 1.3 在VS2017中使用 1.3.1 在VS201 ...

  8. 检测到 #include 错误。请更新 includePath。已为此翻译单元(E:\abc.ino)禁用波形曲线。C/C++(1696) 无法打开 源 文件 “stadio.h“

    我们在配置vscode时,测试代码时,可能会出现这种情况. 检测到 #include 错误.请更新 includePath.已为此翻译单元(E:\abc.ino)禁用波形曲线.C/C++(1696) ...

  9. Python忽略warning警告错误

    Python忽略warning警告错误 示例代码1: import warningswarnings.filterwarnings('ignore') 示例代码2: import warningswa ...

最新文章

  1. CentOS6.5 缺少 libstdc++.so.6(GLIBCXX_3.4.15)
  2. HDU1823(二维线段树)
  3. Identity Server 4 - Hybrid Flow - Claims
  4. java switch finally_JavaSE
  5. python 随机数_Python中的随机数
  6. python基础代码大全-python基础语法,python 代码命令大全
  7. Problem 2128 最长子串
  8. 使用之后,谈谈国产芯片的体验
  9. 二阶无源低通滤波器幅频特性曲线_二阶无源滤波器
  10. python线性链表
  11. java isbn_JAVA ISBN10 ISBN13 正则表达式
  12. 游戏界面设计艺术性的思考
  13. Vue 利用后端的数据字典和Map对象实现表格列字段动态转义的处理方案
  14. 用AW国际版激活国行moto二代
  15. VS6 sp6补丁下载 [防VC6卡死]
  16. vue 的computed和watch在什么时候触发
  17. C++实现OTSU算法(大津法)
  18. Nginx 又一牛X的功能!流量拷贝
  19. 共享单车需求分析(一)
  20. Boot failure on device

热门文章

  1. php 常用文件系统函数,PHP fileperms 文件系统函数
  2. 没信号也不怕?iPhone 14或将支持卫星网络连接呼救
  3. 大动作!今日头条、西瓜视频并入抖音,字节梁汝波正式接任CEO
  4. 国家邮政局:9月份全国快递服务企业业务收入完成921.4亿元,同比增长11.8%
  5. 微软宣布明年停止支持已推出25年的IE浏览器
  6. iPhone 12s渲染图曝光,刘海变小了!
  7. 掏出来的“耳屎经济”
  8. 开到朝鲜的国产十元店,一年如何卖出190亿?
  9. 因未能提交年度报告 瑞幸咖啡收到纳斯达克退市通知
  10. 官方晒一加8系列旗舰关键配置:号称更极致的屏幕机皇