You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何clang-tidy对我的C++代码报未初始化值错误?

clang-tidy报告未初始化值错误排查

我无法理解我的代码为何存在问题,我已确保初始化了二维数组中的所有值,理论上不该出现未初始化值错误!

注意:本项目中我不能使用std::vector,也因无法导入algorithm库,改用自定义函数替代std::min。

clang-tidy报告的错误如下:

第二个函数调用参数是未初始化的值

以下是我的代码(错误仅出现在第17行):

int** SeamCarver::CarvingSeamVertical() const {
  int** energies = new int*[height_];
  for (unsigned int row = 0; row < height_; row++) {
    energies[row] = new int[width_];
    for (unsigned int col = 0; col < width_; col++) {
      energies[row][col] = 0; // initializes every value in the 2D array to zero
    }
  }
  for (unsigned int col = 0; col < width_; col++) {
    energies[height_ - 1][col] = this->GetEnergy(height_ - 1, (int)col);
  }

  for (int row = height_ - 2; row >= 0; row--) {
    for (int col = 0; col < width_; col++) {
      int best = 0;
      if (col == 0) { //error occurs here v
        best = FindMinTwo(energies[row + 1][col], energies[row + 1][col + 1]);
      } else if (col == width_ - 1) {
        best = FindMinTwo(energies[row + 1][col - 1], energies[row + 1][col]);
      } else {
        best = FindMinThree(energies[row + 1][col - 1],
                            energies[row + 1][col],
                            energies[row + 1][col + 1]);
      }
      energies[row][col] = this->GetEnergy(row, col) + best;
    }
  }
  return energies;
} // energies is properly deallocated by the function that uses it

我尝试过将int改为unsigned int,但错误仍未消失。此外我也不应出现越界访问错误,因为row从height_-2开始,循环内访问的最高row值为height_-1。
奇怪的是,添加打印数组所有值的语句后,错误就消失了。

内容的提问来源于stack exchange,提问作者The_Rogue12

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 18:57:08