【问题标题】:Valgrind Error - Address 0x0 is not stack'd malloc'd or free'dValgrind 错误 - 地址 0x0 未堆栈 malloc'd 或 free'd
【发布时间】:2019-12-07 22:19:13
【问题描述】:

我正在尝试根据主行参数中声明的列数和行数分配内存。例如 a.out 2 4 6 意味着运行 a.out 有 2 个玩家,4 列,6 行。

我希望使用 board_init 函数分配内存,但 valgrind 返回: user_input_players(int, char**) 的读取大小 1 无效 并且堆分配了 0,有 0 个空闲,并且没有泄漏的可能。

我只熟悉 new 和 delete 命令。我不确定如何理解错误消息。

// function declaration
int user_input_players(int choices, char** selection);
int user_input_columns(int choices, char** selection);
int user_input_rows(int choices, char** selection);
int board_init(int columns, int rows);


int main(int argc, char** argv) {
 int column = 0, row = 0, player = 0;

 player = user_input_players(argc, argv);
 column = user_input_columns(argc, argv);
 row = user_input_rows(argc, argv);
 board_init(column, row);
 return 0;
}

int user_input_players(int choices, char** selection){
  int player_count = 0;
  for(int i = 0; i < choices; i++){
    selection[i];
  }
  while( !(*selection[1] == '1' || *selection[1] == '2')){
    cout << "Please enter number of players: " << endl;
    cin.clear();
    cin >> player_count;
   if(player_count == 1 || player_count == 2){
    return player_count;
   }
  player_count = *selection[1];
  return player_count;
 }
}

int user_input_columns(int choices, char** selection){
 int column_count = 0;
 for(int i = 0; i < choices; i++){
  selection[i];
  }
 column_count = atoi(selection[2]);
 if(column_count >= 4 && column_count <= 20){
  return column_count;
  }
 while( !(column_count >= 4 && column_count <= 20)){
  cout << "Please enter number of columns: " << endl;
  cin.clear();
  cin >> column_count;
   if(column_count >= 4 && column_count <= 20){
     return column_count;
    }
  }
}

int user_input_rows(int choices, char** selection){
 int row_count = 0;
 for(int i = 0; i < choices; i++){
   selection[i];
   }
 row_count = atoi(selection[3]);
 if(row_count >= 4 && row_count <= 20){
   return row_count;
  }
 while( !(row_count >= 4 && row_count <= 20)){
    cout << "Please enter number of rows: " << endl;
    cin.clear();
    cin >> row_count;
    if(row_count >= 4 && row_count <= 20){
      return row_count;
    }
  }
}

 void board_init(int columns, int rows){  // Valgrind shows no memory allocation because of error
  int** board = new int*[rows];
  for(int i = 0; i < rows; i++){
   board[i] = new int[columns];
  }
}

【问题讨论】:

  • 您的函数被声明为返回一个值,但在许多情况下您都没有这样做。你在阅读你的编译器错误和警告吗?由于这个原因,代码原样调用未定义的行为。例如,int board_init,但您在哪里返回 int
  • int** board in board_init 为该函数创建一个局部变量。不太可能是你想要的
  • @PaulMcKenzie 我明白,我会解决这些问题。然而,这只是一个 sn-p,代码使用 g++ 编译和运行,没有任何错误。我正在尝试确定为什么我的记忆不起作用。
  • int** board = new int*[rows]; -- 如前所述,这是一个局部变量。

标签: c++ memory valgrind


【解决方案1】:

如果您被允许使用矢量,请将您的 init_board 函数改为使用二维矢量:

vector<vector<int> > vec( row , vector<int> (column));  

这将防止正在发生的内存泄漏。您还应该考虑直接在 main 中执行此操作。或者,如果您想保持函数的原样,您应该考虑从函数中返回 int** 并将其存储到 main 中的 int** 变量中,以便您以后可以将其删除。目前您存在内存泄漏,因为您从未删除在 board_init 中分配的内存。

【讨论】:

  • 我不能使用向量,因为这是一个班级作业。另外,很抱歉这不清楚,没有 board_init 就会发生错误。我期待 valgrind 返回分配的内存警告并且它还没有被释放,因为我还没有删除它。
猜你喜欢
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
  • 1970-01-01
相关资源
最近更新 更多