我找不到确切问题的答案,因为条件更具体一点:如何分配结构指针数组
typedef struct COORDS
{
int xp;
int yp;
} coord;
coord** xy;
我想分配它像:xy[500][460]
,但它返回无效的内存错误时访问它们。
coord** new = malloc (500 * sizeof (coord*));
int idx = 0;
for(; idx < 500; ++idx)
new [idx] = malloc (460 * sizeof (coord));
静态分配:
coord xy[500][460];
动态分配:
coord** xy = (coord**)malloc(sizeof(coord*)*500);
for (int i=0; i<500; i++)
xy[i] = (coord*)malloc(sizeof(coord)*460);
// and at a later point in the execution of your program
for (int i=0; i<500; i++)
free(xy[i]);
free(xy);