Static analysis of C/C++ code using cppcheck

Download cppcheck from here:
http://cppcheck.sourceforge.net/
some of the things you can find out by running cppcheck on your code is:
1) Summary: Possible inefficient checking for ‘idList’ emptiness.
Message: Checking for ‘idList’ emptiness might be inefficient. Using idList.empty() instead of idList.size() can be faster. idList.size() can take linear time but idList.empty() is guaranteed to take constant time.
2) Scope:
Summary: The scope of the variable ‘l’ can be reduced
Message: The scope of the variable ‘l’ can be reduced. Warning: It can be unsafe to fix this message. Be careful. Especially when there are inner loops. Here is an example where cppcheck will write that the scope for ‘i’ can be reduced:


void f(int x)
{
    int i = 0;
    if (x) {
        // it's safe to move 'int i = 0' here
        for (int n = 0; n < 10; ++n) {
            // it is possible but not safe to move 'int i = 0' here
            do_something(&i);
        }
    }
}
When you see this message it is always safe to reduce the variable scope 1 level.

3) PostFix and PreFix operators:
Better to use prefix operators.
Summary: Prefer prefix ++/– operators for non-primitive types.
Message: Prefix ++/– operators should be preferred for non-primitive types. Pre-increment/decrement can be more efficient than post-increment/decrement. Post-increment/decrement usually involves keeping a copy of the previous value around and adds a little extra code.
4) Summary: Function parameter ‘sFile’ should be passed by reference.
Message: Parameter ‘sFile’ is passed as a value. It could be passed as a (const) reference which is usually faster and recommended in C++.
5) Assigning values that are never used.
6) Extra qualification in terms of declaring function names inside a class.


class A{
int A::runClass();
}

In

Leave a Reply

Your email address will not be published. Required fields are marked *