在函数之间传递堆上的数组时,是什么导致了这个运行时错误,c++

What is causing this runtime error while passing arrays on the heap between functions, c++

本文关键字:是什么 c++ 运行时错误 之间 函数 数组      更新时间:2023-10-16

当我在接受双指针的函数之间传递堆上声明的2d数组时,我无法理解为什么我的程序挂起并崩溃。

我有一种强烈的感觉,这与我选择的声明2d数组的方法有关。在我创建一个函数来分配数组之前,程序可以在传递给函数时处理数组中的数据。

这是分配函数,然后是它内部崩溃的函数:

void matrix_malloc(int **matrix, int m, int n);
void matrix_init(int **matrix, int m, int n);
int main(void)
{
  int **matrix;
  int m(3), n(2);
  matrix_malloc(matrix, m, n);
  matrix_init(matrix, m, n); // runtime error
}
void matrix_malloc(int **matrix, int m, int n)
{ // get heap memory
  int i;
  matrix = new int*[m];
  for(i = 0; i < m; i++)
  {
    matrix[i] = new int[n];
  }
}
void matrix_init(int **matrix, int m, int n)
{ // randomize matrix
  int i, j;
  for(i = 0; i < m; i++)
  {
    for(j = 0; j < n; j++)
    {
      matrix[i][j] = rand() % 10 + 1;
    }
  }
}

您必须通过引用传递矩阵指针。

void matrix_malloc(int **matrix, int m, int n)

这接受矩阵的副本。这意味着您在matrix_malloc中对matrix所做的任何操作都不会影响main中的操作。

相反,它应该是

void matrix_malloc(int **& matrix, int m, int n)
                        ^^^

但是,我建议您使用向量,而不是原始指针和分配。这样你就不需要担心分配和交易。

void matrix_malloc(vector<vector<int> >& matrix, int m, int n);
// You don't need this anymore.
// void matrix_init(int **matrix, int m, int n);
int main(void)
{
  vector<vector<int> > matrix;
  int m(3), n(2);
  // matrix_malloc(matrix, m, n);
  matrix_init(matrix, m, n); 
}

void matrix_init(vector<vector<int> >& matrix, int m, int n)
{ // randomize matrix
  int i, j;
  for(i = 0; i < m; i++)
  {
    vector<int> row;
    for(j = 0; j < n; j++)
    {
      row.push_back(rand() % 10 + 1);
      // matrix[i][j] = rand() % 10 + 1;
    }
    matrix.push_back(row);
  }
}
void matrix_malloc(int **&matrix, int m, int n);
void matrix_init(int **matrix, int m, int n);
void matrix_malloc(int **&matrix, int m, int n)
{ // get heap memory
  int i;
  matrix = new int*[m];
  for(i = 0; i < m; i++)
  {
    matrix[i] = new int[n];
  }
}

而且应该工作得很好。问题,因为在这个之后

matrix = new int*[m];

矩阵有新的地址,但由于它是指针的本地副本,所以main不知道它。

matrix_malloc()需要通过引用获取指针:

void matrix_malloc(int **&matrix, int m, int n)
                         ^

如果没有这一点,新分配的指针就不会传播回调用方。

也就是说,返回函数中新分配的指针可能更明确:

int** matrix_malloc(int m, int n)

最后,您不使用std::vector的原因是什么?

2D数组的分配很好。但是。

  int **matrix;
  int m(3), n(2);
  matrix_malloc(matrix, m, n);

在这里,矩阵不会改变-您正在复制它的值以将其传递给函数。我的意思是:

  int **matrix = NULL; // matrix points to null
  int m(3), n(2);
  matrix_malloc(matrix, m, n); // copy the value contained in matrix and give it to the function
  //matrix still points to null

您有多种解决方案:

  • 矩阵malloc可以返回一个int**,您只需要写矩阵=矩阵_ malloc(m,n(
  • 您的矩阵malloc可以使用指向int**的指针(int***-小心处理(
  • 如其他答案中所述,对int的引用**

下面是一个带int***的matrix_malloc的样子。

//call it as follows:
matrix_malloc(&matrix, m, n);

void matrix_malloc(int ***matrix, int m, int n)
{
  // matrix contains the address of the original variable, so *matrix is the original variable itself.
  int i;
  *matrix = new int*[m];
  for(i = 0; i < m; i++)
  {
    (*matrix)[i] = new int[n];
  }
}