Static analyzers
Static analyzers are tools that go through source code and detect potential issues with the code such as undefined behavior, or they check whether the code is compliant with a safety standard such as MISRA® or AUTOSAR®. Not all static analyzers have the same capabilities, and only commercial versions support safety standards checks. Some of the issues that can be detected with static analyzers are as follows:
- Use of uninitialized data
- Out-of-bounds array access
- Null pointers dereference
- Division by zero
- Use after delete, double delete, and other memory management issues
We can enable GCC’s static analyzer by providing the GCC driver program with the –fanalyzer
flag. Let us take an example of a simple sum function that takes a std::array<int, 4>
constant reference and returns the sum shown in the following example:
#include <array>
int sum(const std::array<int, 4> &arr) {...