问题描述
我正在尝试在 C 程序中分配一个二维数组.它在这样的主要功能中工作正常(如 这里):
I'm trying to allocate a 2d array in a C program. It works fine in the main function like this (as explained here):
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char ** argv)
{
int ** grid;
int i, nrows=10, ncols=10;
grid = malloc( sizeof(int *) * nrows);
if (grid == NULL){
printf("ERROR: out of memory
");
return 1;
}
for (i=0;i<nrows;i++){
grid[i] = malloc( sizeof(int) * ncols);
if (grid[i] == NULL){
printf("ERROR: out of memory
");
return 1;
}
}
printf("Allocated!
");
grid[5][6] = 15;
printf("%d
", grid[5][6]);
return 0;
}
但是由于我必须使用不同的数组多次执行此操作,因此我试图将代码移至单独的函数中.
But since I have to do this several times with different arrays, I was trying to move the code into a separate function.
#include <stdio.h>
#include <stdlib.h>
int malloc2d(int ** grid, int nrows, int ncols){
int i;
grid = malloc( sizeof(int *) * nrows);
if (grid == NULL){
printf("ERROR: out of memory
");
return 1;
}
for (i=0;i<nrows;i++){
grid[i] = malloc( sizeof(int) * ncols);
if (grid[i] == NULL){
printf("ERROR: out of memory
");
return 1;
}
}
printf("Allocated!
");
return 0;
}
int main(int argc, char ** argv)
{
int ** grid;
malloc2d(grid, 10, 10);
grid[5][6] = 15;
printf("%d
", grid[5][6]);
return 0;
}
然而,虽然它在分配时没有抱怨,但在访问数组时出现分段错误.我阅读了关于衰变数组和类似主题的不同帖子,但我仍然无法弄清楚如何解决这个问题.我想我没有正确地将二维数组传递给函数.
However, although it doesn't complain while allocating, I get segmentation fault when accessing the array. I read different posts on decayed arrays and similar topics, but I still can't figure out how to solve this problem. I imagine I'm not passing the 2d array correctly to the function.
非常感谢.
推荐答案
那不是多维数组;它是一个一维数组,包含指向一维数组的指针.多维数组不包含指针;它们是单个内存块.
That is not a multidimensional array; it is a single dimensional array containing pointers to single dimensional arrays. Multidimensional arrays do not contain pointers; they are single memory blocks.
您的问题是您有一个指向指针的指针,并且您正试图通过参数从您的函数中返回它.如果你打算这样做,你将需要一个指向一个指针的指针作为你的参数,你将不得不将一个指针的地址传递给一个指向该方法的指针.如果不这样做,则不会更改 main
中变量 grid
的值——而是将作为参数复制的值更改为malloc2d
函数.由于 main
中的 grid
未初始化,您会得到未定义的行为.
Your problem here is that you have a pointer to a pointer, and you're trying to return it from your function through a parameter. If you're going to do that, you're going to need a pointer to a pointer to a pointer as your parameter, and you're going to have to pass the address of a pointer to a pointer to the method. If you don't do this, you're not changing the value of the variable grid
in main
-- you're changing the one which was copied as the parameter to the malloc2d
function. Because the grid
in main
is left uninitialized, you get a undefined behavior.
以下是我所说的修复示例:
Here's an example of what I mean as the fix:
#include <stdio.h>
#include <stdlib.h>
int malloc2d(int *** grid, int nrows, int ncols){
int i;
*grid = malloc( sizeof(int *) * nrows);
if (*grid == NULL){
printf("ERROR: out of memory
");
return 1;
}
for (i=0;i<nrows;i++){
(*grid)[i] = malloc( sizeof(int) * ncols);
if ((*grid)[i] == NULL){
printf("ERROR: out of memory
");
return 1;
}
}
printf("Allocated!
");
return 0;
}
int main(int argc, char ** argv)
{
int ** grid;
malloc2d(&grid, 10, 10);
grid[5][6] = 15;
printf("%d
", grid[5][6]);
return 0;
}
附加说明:
- 如果单个分配失败,则会泄漏第一个数组的分配以及所有先前行的分配.您需要在返回之前调用
free
. - 您通过一个参数返回,即使您真的没有必要这样做.如果我在写这个,我会让方法返回
int **
,并通过返回0
来表示错误.
- If a single allocation fails, you leak the allocation for the first array, as well as the allocations for all previous rows. You need to call
free
on those before returning. - You're returning through a parameter, even though you really don't have to. If I were writing this, I'd make the method return
int **
, and signal error by returning0
.
这篇关于在函数中分配多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!