Cppcheck – A free static analyser for C and C++

I got a tip from Anna-Jayne Metcalfe of C++ and QA specialists Riverblade to check out Cppcheck, a free static analyser for C and C++. I ran >100 kLOC of PerfectTablePlan C++ through it and it picked up a few issues, including:

  • variables uninitialised in constructors
  • classes passed by value, rather than as a const reference
  • variables whose scopes could be reduced
  • methods that could be made const

It only took me a few minutes from downloading to getting results. And the results are a lot less noisy than lint. I’m impressed. PerfectTablePlan is heavily tested and I don’t think any of the issues found are the cause of bugs in PerfectTablePlan, but it shows the potential of the tool.

The documentation is here. But, on Windows, you just need to start the Cppcheck GUI (in C:\Program files\Cppcheck, they appear to be too modest to add a shortcut to your desktop), select Check>Directory… and browse to the source directory you want to check. Any issues found will then be displayed.

You can also set an editor to integrate with, in Edit>Preferences>Applications. Double clicking on an issue will then display the appropriate line in your editor of choice.

Cppdepend is available with a GUI on Windows and as a command line tool on a range of platforms. There is also an Eclipse plugin. See the sourceforge page for details on platforms and IDEs supported. You can even write your own Cppcheck rules.

Cppcheck could be a very valuable additional layer in my defence in depth approach to QA. I have added it to my checklist of things to do before each new release.

9 Responses to “Cppcheck – A free static analyser for C and C++”


  1. 1 Bartosz Wójcik 29 November 2011 at 9:43 am

    I recommend Parasoft C++Test (trial)

  2. 3 Steve Hanov 29 November 2011 at 1:10 pm

    It seems like something that should be done every build, not once per release. Hopefully there is a command line interface as well.

    • 4 Andy Brice 29 November 2011 at 1:52 pm

      Probably a good idea to run it after each major new feature is added. I don’t think I could be bothered to look at the results after every check-in.

      There is a command line interface.

  3. 5 CoreTech 2 December 2011 at 5:19 am

    Thanks for letting me know about this great tool! It was easy to install and it turned up some perfectly valid (though mostly benign) warts on my code.

    I am particularly sensitive to ‘const’-ness & variable scoping and I was pleased to receive actionable advice on both of those.

  4. 6 Ross 4 December 2011 at 10:35 am

    Thanks for the reference. I develop Qt/C++ application and was thinking for alternative for Valgrind alternative for Windows.

  5. 8 John Harris 31 December 2011 at 11:55 pm

    Hi Andy,

    Great post, I want to try this one to address memory leaks, i’m developing a wordpress hosting like the one’s seen here: : http://easywordpressblogsetupinstructions.com/2011-our-list-of-5-top-wordpress-hosting-companies/

    Any other alternatives to cppcheck? I heard about EDoC++ and Flawfinder?

  6. 9 Andy Brice 5 January 2012 at 12:01 am

    CppCheck isn’t likely to be very useful for finding memory leaks. You need a dynamic analyser. See above.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s




Enter your email address to follow this blog and receive notifications of new posts by email.

Join 454 other followers

Blog Stats

  • 1,396,722 hits
When you are developing a software product it can be hard to
"see the forest for the trees"
see the forest for the trees
Do you need some affordable, independent advice on where to go next with your product?
Countdown the days, hours, minutes and seconds to your next important event with our free countdown clock for Windows or web.
free countdown clock

Categories

Creative Commons License
This work is licenced under a Creative Commons Licence.

Follow

Get every new post delivered to your Inbox.

Join 454 other followers