PVS-Studio for checking labs in C and C++
I met another question on Stack Overflow from a person who studies the C ++ language. The number of such questions can be reduced by using PVS-Studio. A person can get an answer right away without distracting others.
I have already described here, that the online version of the PVS-Studio analyzer can make life much easier for novice programmers. Let’s consider another similar case.
Let’s take a look at the discussionC++ error: “pointer being freed was not allocated” on the Stack Overflow site and look at this code:
#include <stdexcept>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
using std::cout;
using std::endl;
using std::vector;
typedef vector<int> ints;
void print_ints(vector<int>);
void print_ints_vec(vector<vector<int>>);
void int_part(int, vector<vector<int>>&);
int main()
{
vector<vector<int>> partition;
int_part(5, partition);
print_ints_vec(partition);
return 0;
}
void int_part(int sum, vector<vector<int>>& res)
{
vector<int> init_xs = vector<int>{sum};
vector<int>* xs = &init_xs; // POINTER INITIALIZED TO vector<int>
int current_sum = sum;
while (true)
{
current_sum = accumulate(xs->begin(), xs->end(), 0);
if (current_sum == sum)
{
res.push_back(*xs);
vector<int> next_xs;
vector<int>::iterator it = find(xs->begin(), xs->end(), 1);
if (it == xs->begin()) return;
copy(xs->begin(), it, back_inserter(next_xs));
next_xs[next_xs.size() - 1] -= 1;
xs = &next_xs; // POINTER REASSIGNED TO ANOTHER vector<int>
}
else
{
int tail = xs->back();
int diff = sum - current_sum;
int m = std::min(tail, sum - tail);
int next_tail = current_sum + m > sum ? diff : m;
xs->push_back(next_tail);
}
}
}
void print_ints(ints v) // PRINT UTILITY
{
cout << "[ ";
for (const int& n : v) { cout << n << "; "; }
cout << "]" << endl;
}
void print_ints_vec(vector<ints> v) // PRINT UTILITY
{
cout << "[ \n";
for (const vector<int>& xs : v) { cout << " "; print_ints(xs); }
cout << "]" << endl;
}
Agree that you don’t really want to waste time and effort on reading this text of a laboratory work, looking for an error. And it is not necessary! Let’s entrust this task to the PVS-Studio analyzer.
That’s what he informs: V506 Pointer to local variable ‘next_xs’ is stored outside the scope of this variable. Such a pointer will become invalid.
Line:
xs = &next_xs; // POINTER REASSIGNED TO ANOTHER vector<int>
Indeed, the error is that a reference to an object that will be destroyed is saved. This error was pointed out by other people and explained what the problem is. However, one could do without waiting for a response from more experienced colleagues. After all, an explanation of the error can be obtained from the PVS-Studio analyzer documentation on diagnostics V506.
Conclusion
Keep in mind the scenario of using PVS-Studio for training tasks. The information provided by the analyzer can help in learning by telling the person what is wrong with their code. Of course, this does not replace other people’s code reviews. After all, a person will not only find an error, but will give recommendations on how best to write code. Nevertheless, static analysis is still a good quick assistant when learning to program.
Additional links:
- Static Code Analysis.
- PVS Studio: online version.
- PVS Studio: free use for students.
If you want to share this article with an English-speaking audience, please use the translation link: Andrey Karpov. PVS-Studio to help with schoolwork-like tasks in C and C++.