问题
I am using the below code snippet to allocate memory for 2D array using minimum number of malloc() calls.
I want to access the array using subscripts, p[i][j].
#define ROW 3
#define COL 2
int main()
{
void **ptr = malloc( ROW*COL* sizeof(int) );
int (*p)[COL] = ptr;
int i, j;
for( i = 0; i < ROW; ++i )
for( j = 0; j < COL; ++j )
scanf("%d", &ptr[i][j]);
for( i = 0; i < ROW; ++i )
{
for( j = 0; j < COL; ++j )
printf("%d ", p[i][j]);
printf("\n");
}
return 0;
}
The program is outputting correctly whatever is the input.
But, it is showing Runtime error . Why?
回答1:
If the array dimensions are known at compile-time (as in your example), then you can indeed allocate memory in one malloc call. But you have to use the proper pointer type to access that memory. In your case that would be your p pointer. You p pointer is declared correctly, but you for some reason are completely ignoring its existence in scanf and using ptr instead.
Stop trying to use ptr for array access. Use p. Access your array elements as p[i][j] and it should work.
In fact, I would get rid of ptr entirely and do memory allocation in the following way
int (*p)[COL] = malloc(ROW * sizeof *p);
Moreover, since both dimensions are known at compile time, you can actually allocate it as
int (*p)[ROW][COL] = malloc(sizeof *p);
but in this case you'll have to remember to access the array as (*p)[i][j] (note the *). Choose whichever method you prefer.
回答2:
Try this one:
#define ROW 3
#define COL 2
int main()
{
int *ptr = (int*)malloc( ROW*COL* sizeof(int) );
int *p[COL];
int i, j;
fot (i = 0; i < COL; i++)
p[i] = ptr + ROW * i;
for( i = 0; i < ROW; ++i )
for( j = 0; j < COL; ++j )
scanf("%d", &p[i][j]);
for( i = 0; i < ROW; ++i )
{
for( j = 0; j < COL; ++j )
printf("%d ", p[i][j]);
printf("\n");
}
free(ptr);
return 0;
}
来源:https://stackoverflow.com/questions/12133832/allocate-memory-for-2d-array-with-minimum-number-of-malloc-calls