c ++ Klocwork错误内存是通过调用'new[]'分配的

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

Klocwork 2020.1 版本 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;
        }
    }

..... 这就是内存空闲的方式

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);

这是KW错误获取

我不明白这个问题,也找不到任何修复建议。

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

我找不到任何修复建议。

修复错误“memory is allocated through the call to new[]”就是不通过new[]分配内存。最简单的解决方案是改用 std::vector