>
>
>
PVS-Studio to help with schoolwork-like…

Andrey Karpov
Articles: 673

PVS-Studio to help with schoolwork-like tasks in C and C++

Today I'll talk some more about questions posted on Stack Overflow — in particular, about another discussion started by someone learning the C++ language. I'd like to note that, if you are just learning to code, PVS-Studio can be of great help. It'll answer many of your questions — and you won't need to wait for others to answer you on Stack Overflow!

In my previous article, I described how the PVS-Studio analyzer's online version can make life easier for novice programmers. Now I'll review a similar case.

It's a discussion I found on Stack Overflow: "C++ error: "pointer being freed was not allocated". Let's investigate the 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, to an average expert from Stack Overflow, inspecting the code above line by line is boring — especially given the fact that the code in question is expected to solve an easy schoolwork-like task. But actually, there's no need to distract experts and to wait for their answer. Let have the PVS-Studio analyzer examine the code instead!

And here's what it reports: V506 Pointer to local variable 'next_xs' is stored outside the scope of this variable. Such a pointer will become invalid.

Here's the line where the analyzer found the problem:

xs = &next_xs; // POINTER REASSIGNED TO ANOTHER vector<int>

And the analyzer is indeed correct. The code saves a reference to an object. Then this object is destroyed. People on Stack Overflow also pointed out and explained this error. However, one doesn't need to wait for when more experienced peers can investigate an issue and reply. In this case, one can learn everything about the error in the PVS-Studio documentation on the V506 diagnostic.

Conclusion

As you can see from the example above, one can use PVS-Studio when learning to code. The analyzer's warnings can help beginner developers figure out what's wrong in their code. Of course, this is not a replacement for a real code review done by experts or peers. Aside from finding errors, a developer can give tips on improving code. However, static analysis is still a quick and effective tool one can use when learning to code.

Additional resources: