我使用的是带有标志符-Wall -std=gnu99
的gcc版本4.8.0。
我需要在C中使用malloc动态分配连续二维数组的内存;这一事实不容置疑。但是,为了便于使用,我仍然希望能够使用方便的x[r][c]
符号访问数组。这是我勇敢的尝试创建一个指向连续二维数组的指针,并通过执行*array[r][c]
:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
size_t rows = 3, cols = 5;
printf("sizeof(int) = %linn", sizeof(int));
int (*array)[rows][cols] = malloc(sizeof(int) * rows * cols);
printf("array starts at %pn", array);
printf("sizeof(array) = %lin", sizeof(array));
printf("sizeof(array[0][0]) = 0x%lxn", sizeof(array[0][0]));
puts("");
unsigned short r, c;
for (r = 0; r <= rows - 1; r++) {
for (c = 0; c <= cols - 1; c++) {
printf("array[%i][%i] is at %pn", r, c, &(*array[r][c]));
};
puts("");
};
}
虽然它编译时没有警告,但它有一些意想不到的输出:
sizeof(int) = 4
array starts at 0x16cc010
sizeof(array) = 8
sizeof(array[0][0]) = 0x14
array[0][0] is at 0x16cc010
array[0][1] is at 0x16cc024
array[0][2] is at 0x16cc038
array[0][3] is at 0x16cc04c
array[0][4] is at 0x16cc060
array[1][0] is at 0x16cc04c
array[1][1] is at 0x16cc060
array[1][2] is at 0x16cc074
array[1][3] is at 0x16cc088
array[1][4] is at 0x16cc09c
array[2][0] is at 0x16cc088
array[2][1] is at 0x16cc09c
array[2][2] is at 0x16cc0b0
array[2][3] is at 0x16cc0c4
array[2][4] is at 0x16cc0d8
我真的不明白这里发生了什么。元素是连续的,但是对于整型来说它们太大了,而数组又太小了,容纳不了所有的元素。
我的数组声明是我最不确定的部分-我正在阅读int (*array)[rows][cols]
作为"array
是指向整数数组的指针(stride cols
)",但显然这是不正确的。
我到底做错了什么?
int (*array)[cols] = malloc( sizeof *array * rows );
for (r = 0; r <= rows - 1; r++) {
for (c = 0; c <= cols - 1; c++) {
printf("array[%i][%i] is at %pn", r, c, &array[r][c]);
这允许您将array
索引为array[i][j]
,因为array[i]
隐式地解引用指针。
要解除分配,只需要调用
free( array );
代码应为
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
size_t rows = 3, cols = 5;
printf("sizeof(int) = %linn", sizeof(int));
int (*array)[rows][cols] = malloc(sizeof(int) * rows * cols);
printf("array starts at %pn", array);
printf("sizeof(array) = %lin", sizeof(*array)); // Note the *
printf("sizeof(array[0][0]) = 0x%lxn", sizeof((*array)[0][0]));
puts("");
unsigned short r, c;
for (r = 0; r <= rows - 1; r++) {
for (c = 0; c <= cols - 1; c++) {
printf("array[%i][%i] is at %pn", r, c, &((*array)[r][c]));
};
puts("");
};
}
由于数组是指向数组[x][y]的指针,因此必须首先对其解引用
(*array)
访问项
(*array)[x][y]
这样就得到
sizeof(int) = 4
array starts at 0x12de010
sizeof(array) = 60
sizeof(array[0][0]) = 0x4
array[0][0] is at 0x12de010
array[0][1] is at 0x12de014
array[0][2] is at 0x12de018
array[0][3] is at 0x12de01c
array[0][4] is at 0x12de020
array[1][0] is at 0x12de024
array[1][1] is at 0x12de028
array[1][2] is at 0x12de02c
array[1][3] is at 0x12de030
array[1][4] is at 0x12de034
array[2][0] is at 0x12de038
array[2][1] is at 0x12de03c
array[2][2] is at 0x12de040
array[2][3] is at 0x12de044
array[2][4] is at 0x12de048
在预期的值范围内