开源C++/C代码检查工具

Cppcheck
cppcheck是静态的C/C++ 代码分析工具,用以检查内存泄漏,错配的内存分配和释放,缓冲区溢出等问题。支持eclipse插件。
Someof the checks that are supported include:css

http://en.wikipedia.org/wiki/Cppcheckhtml


Splint:程序员

Splint是静态检查C语言安全弱点和编写错误的程序。检查主要包括:未使用的变量,类型不一致,使用未定义的变量,内存管理错误(内存泄露,悬浮指针等),缓冲区溢出,没法执行到的程序,忽略返回值,无限循环等。缓存

Problemsdetected by Splint include:安全

  •  Dereferencing a possibly null pointer (Section 2);
  •  Using possibly undefined storage or returning storage that is not properly defined (Section 3);
  •  Type mismatches, with greater precision and flexibility than provided by C compilers (Section 4.1–4.2);
  •  Violations of information hiding (Section 4.3);
  •  Memory management errors including uses of dangling references and memory leaks  (Section 5);
  •  Dangerous aliasing (Section 6);
  •  Modifications and global variable uses that are inconsistent with specified interfaces (Section 7);
  •  Problematic control flow such as likely infinite loops (Section 8.3.1), fall through cases or incomplete switches (Section 8.3.2), and suspicious statements (Section 8.4);
  •  Buffer overflow vulnerabilities (Section 9);
  •  Dangerous macro implementations or invocations (Section 11); and
  •  Violations of customized naming conventions.  (Section 12)
    .源文档 <<A href="http://splint.org/manual/html/sec1.html">http://splint.org/manual/html/sec1.html>

通过初步测试splint相对于cppcheck更为强大,并且当前版本更为稳定。eclipse

 

Valgrind:ide

Valgrind是一个用于检查程序内存泄漏、段错误等bug的工具。它包含的有:内存检测、缓存检测、代码覆盖、性能测试等功能。Valgrind的最初做者是Julian Seward,他于2006年因为在开发Valgrind上的工做得到了第二届Google-O'Reilly开源代码奖。函数

Valgrind是运行在 Linux上一套基于仿真技术的程序调试和分析工具,它包含一个内核——一个软件合成的CPU,和一系列的小工具,每一个工具均可以完成一项任务──调试,分析,或测试等。Valgrind能够检测内存泄漏和内存违例,还能够分析cache的使用等,灵活轻巧而又强大,能直穿程序错误的心脏,真可谓是程序员的瑞士军刀。它包括Memcheck,Callgrind, Cachegrind, helgrind等一些列的工具。工具

Memcheck是其中最经常使用的工具,用来检测程序中出现的内存问题,全部对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获。因此,它能检测如下问题:oop

1.对未初始化内存的使用;

2.读/写释放后的内存块;

3.读/写超出malloc分配的内存块;

4.读/写不适当的栈中内存块;

5.内存泄漏,指向一块内存的指针永远丢失;

6.不正确的malloc/free或new/delete匹配;

7,memcpy()相关函数中的dst和src指针重叠。

 

一些经常使用的选项:

       查考其帮助 valgrind -h       or   info valgrind

 

咱们经过例子看一下它的具体使用。咱们构造一个存在内存泄漏的C程序,以下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

 

 

 

 

 create by guolb57 valgrind_test.c  

 gcc -Wall -g -o valgrind_test -o valgrind_test.c

 use valgrind: 

       valgrind --tool=memcheck ./valgrind_test 

or   valgrind --leak-check=full ./valgrind_test

 

 

 

#include  

#include  

   

int valgrind_test() 

    int *p = malloc(10 * sizeof(int)); 

   

    p[10] = 0;    

   

    return 0; 

  

   

int main(int argc, const char *argv[]) 

    valgrind_test(); 

    printf("hello world\n"); 

    return 0; 

}

咱们获得以下错误信息:

1340187959_6861.jpg

 

咱们能够很清楚地看出,分配和释放了多少内存,有多少内存泄漏。

这对咱们查找内存泄漏十分方便。而后咱们从新编译程序并绑定调试器:

 

Artistic Style Eclipse Plugin 是一个用来对C/C++代码进行格式化的 Eclipse 插件,可在 Eclipse CDT 环境中使用.能够用来定义基本的编码格式风格,好比代码缩进,在* +等运算符两端加上空格的,不一样模块间添加空行

CppNcss复杂度检查工具,可度量函数级,文件级,工程级的复杂度

 

总结:cppcheck与splint是静态的代码检查工具,valgrind为动态的代码检查工具。主要检查对象都是内存管理错误。valgrind因为前二者,它获得更为丰富准确的信息。可是因为它提供了大量的信息,对因而否是bug的鉴定更为困难。ArtisticStyle Eclipse Plugin能在基本的代码格式风格和规范上起到必定的帮助做用。

相关文章
相关标签/搜索