If you have a question, you can ask in our forum.
int main()
{
char a[10];
a[10] = 0;
return 0;
}
If you save that into file1.c and execute:
cppcheck file1.c
The output from cppcheck will then be:
Checking file1.c...
[file1.c:4]: (error) Array index out of bounds
cppcheck path
If "path" is a folder all sourcefiles in it will be checked:
Checking path/file1.cpp...
1/2 files checked 50% done
Checking path/file2.cpp...
2/2 files checked 100% done
By default, only certain errors are reported.
With "--all" you will get more reports. But beware - some messages may be wrong.
Here is a simple code example:
void f()
{
Fred *fred = new Fred;
}
Execute this command:
cppcheck --all file1.cpp
The output from Cppcheck:
[file1.cpp:4]: (possible error) Memory leak: fred
The "possible" means that the reported message may be wrong (if Fred has automatic deallocation it is not a memory leak).
By default Cppcheck will only check for bugs. There are also a few checks for stylistic issues.
Here is a simple code example:
void f(int x)
{
int i;
if (x == 0)
{
i = 0;
}
}
To enable stylistic checks, use the --style flag:
cppcheck --style file3.c
[file3.c:3]: (style) The scope of the variable i can be limited
You can generate XML output:
cppcheck --xml file3.c
You can suppress certain errors:
cppcheck --suppressions=mysuppressions.txt file3.c
If you get false positives about memory leaks because of automatic deallocation:
cppcheck --auto-dealloc autodealloc.lst file4.cpp