Search code examples
c++memoryklocwork

c++ Klocwork error memory is allocated through the call to 'new[]'


Klocwork 2020.1 Build 20.1.0.97

// Vertices of simplex
auto v = new double*[n + 1]; // this line give the error
// Average coordinates
auto v_ave = new double[n];
// Reflection coordinates
auto v_ref = new double[n];
// Expansion coordinates
auto v_exp = new double[n];
// Contraction coordinates
auto v_con = new double[n];

 if (v == nullptr || v_ave == nullptr || v_ref == nullptr || v_exp == nullptr || v_con == nullptr)
 {
     status = false;
     goto free_mem;
  }

    // Allocate the columns of the arrays
    for (int idx = 0; idx <= n; idx++)
    {
        v[idx] = new double[n];
        if (v[idx] == nullptr)
        {
           status = false;
           goto free_mem;
        }
    }

.... this is how the memory is free

free_mem:
    if (v != nullptr)
    {
        // Free memory
        for (int idx = 0; idx <= n; idx++)
        {
            delete[] v[idx];
        }
        delete[] v;
    }

    delete[] v_ave;
    delete[] v_ref;
    delete[] v_con;
    delete[] v_exp;

    return pair<double, bool>(min, status);

this is the KW error get enter image description here

I don't understand the issue and I can't find any suggestion for the fix.


Solution

  • memory is allocated through the call to 'new[]'

    I can't find any suggestion for the fix.

    To fix the error "memory is allocated through the call to new[]" is to not allocate memory through new[]. Simplest solution is to use std::vector instead.