如何在C中声明可变大小的2D数组?

时间:2021-02-16 19:55:08

I have a problem with a project. I have to make a variable size 2D array for storing some prediction error..so this is about images. The trouble is that I have to load images of different sizes so for each image I would have to get into a file the 2D array with the coresponding number of pixels..I've searched among your questions but it's not what I'm looking for.Can anyone help me?

我的项目有问题。我必须制作一个可变大小的2D数组来存储一些预测误差。这是关于图像的。麻烦的是我必须加载不同大小的图像,所以对于每个图像,我必须进入一个文件,相应的像素数的二维数组..我在你的问题中搜索,但它不是我在看什么for.Can有人帮助我吗?

Thank you

谢谢

3 个解决方案

#1


6  

If you have a modern C compiler (at least C99) in function scope it is as simple as:

如果你在函数范围内有一个现代的C编译器(至少是C99),它就像这样简单:

unsigned arr[n][m];

this is called a variable length array (VLA). It may have problems if the array is too large. So if you have large images you could do:

这称为可变长度数组(VLA)。如果阵列太大,可能会出现问题。因此,如果您有大图像,您可以这样做:

unsigned (*arr)[m] = malloc(sizeof(unsigned[n][m]));

and later

然后

free(arr);

#2


2  

If you need the memory to be contiguous, you have a couple of choices.

如果你需要内存是连续的,你有几个选择。

You could dynamically allocate a single block of memory, and then compute your offsets manually, like so:

您可以动态分配单个内存块,然后手动计算偏移量,如下所示:

size_t rows, cols;
...
int *arr = malloc(sizeof *arr * rows * cols);
...
arr[i * rows + j] = ...; // logically equivalent to arr[i][j]

You could set up a second array of pointers into the main array:

您可以在主数组中设置第二个指针数组:

int **arrp = malloc(sizeof *arrp * rows);
...
for (i = 0; i < rows; i++)
  arrp[i] = &arr[i * rows];
...
arrp[i][j] = ...;

remembering that you would have to free both arr and arrp.

记住你必须释放arr和arrp。

If you have a C99 implementation, you can just set up a pointer to a VLA, like so:

如果你有一个C99实现,你可以设置一个指向VLA的指针,如下所示:

int (*arrp)[cols] = (int (*)[cols]) arr;
...
arrp[i][j] = ...;

Note that in this case, you're not allocating any memory for a secondary array, nor do you need to manually compute pointers into the main array; all you have to do is set arrp to the same location as arr and let the rules of pointer arithmetic do all the work.

请注意,在这种情况下,您不是为辅助阵列分配任何内存,也不需要手动计算指向主阵列的指针;你所要做的就是将arrp设置为与arr相同的位置,让指针算术的规则完成所有工作。

If the images aren't that big, you could just set up a VLA (again, C99 or later):

如果图像不是那么大,你可以设置一个VLA(再次,C99或更高版本):

int arr[rows][cols];

but in practice this isn't a good idea; stack frames are usually pretty limited in size.

但在实践中,这不是一个好主意;堆栈框架的大小通常非常有限。

#3


0  

You need to allocate memory dynamically. Use double pointer logic.

您需要动态分配内存。使用双指针逻辑。

Ex:

例如:

int n=10; <<-u can change this.
int **a;
a=(int **)malloc(sizeof(*int)*n);
for (int i=0;i<n;i++){
 a[i]=(int *)malloc(sizeof(int)*n);// or *(a+i)
}

#1


6  

If you have a modern C compiler (at least C99) in function scope it is as simple as:

如果你在函数范围内有一个现代的C编译器(至少是C99),它就像这样简单:

unsigned arr[n][m];

this is called a variable length array (VLA). It may have problems if the array is too large. So if you have large images you could do:

这称为可变长度数组(VLA)。如果阵列太大,可能会出现问题。因此,如果您有大图像,您可以这样做:

unsigned (*arr)[m] = malloc(sizeof(unsigned[n][m]));

and later

然后

free(arr);

#2


2  

If you need the memory to be contiguous, you have a couple of choices.

如果你需要内存是连续的,你有几个选择。

You could dynamically allocate a single block of memory, and then compute your offsets manually, like so:

您可以动态分配单个内存块,然后手动计算偏移量,如下所示:

size_t rows, cols;
...
int *arr = malloc(sizeof *arr * rows * cols);
...
arr[i * rows + j] = ...; // logically equivalent to arr[i][j]

You could set up a second array of pointers into the main array:

您可以在主数组中设置第二个指针数组:

int **arrp = malloc(sizeof *arrp * rows);
...
for (i = 0; i < rows; i++)
  arrp[i] = &arr[i * rows];
...
arrp[i][j] = ...;

remembering that you would have to free both arr and arrp.

记住你必须释放arr和arrp。

If you have a C99 implementation, you can just set up a pointer to a VLA, like so:

如果你有一个C99实现,你可以设置一个指向VLA的指针,如下所示:

int (*arrp)[cols] = (int (*)[cols]) arr;
...
arrp[i][j] = ...;

Note that in this case, you're not allocating any memory for a secondary array, nor do you need to manually compute pointers into the main array; all you have to do is set arrp to the same location as arr and let the rules of pointer arithmetic do all the work.

请注意,在这种情况下,您不是为辅助阵列分配任何内存,也不需要手动计算指向主阵列的指针;你所要做的就是将arrp设置为与arr相同的位置,让指针算术的规则完成所有工作。

If the images aren't that big, you could just set up a VLA (again, C99 or later):

如果图像不是那么大,你可以设置一个VLA(再次,C99或更高版本):

int arr[rows][cols];

but in practice this isn't a good idea; stack frames are usually pretty limited in size.

但在实践中,这不是一个好主意;堆栈框架的大小通常非常有限。

#3


0  

You need to allocate memory dynamically. Use double pointer logic.

您需要动态分配内存。使用双指针逻辑。

Ex:

例如:

int n=10; <<-u can change this.
int **a;
a=(int **)malloc(sizeof(*int)*n);
for (int i=0;i<n;i++){
 a[i]=(int *)malloc(sizeof(int)*n);// or *(a+i)
}