我对这段代码有疑问,它编译正常。但它在内存无法写入错误后立即崩溃。
调试器说问题出在 *grid = (grid_t**)malloc(sizeof(grid_t)*GRID_HEIGHT); ,我一定遗漏了一些明显的东西。
我正在尝试创建一个指向 2D 结构的指针。
#define GRID_WIDTH 12
#define GRID_HEIGHT 22
typedef struct
{
int piece;
int edge;
}grid_t;
grid_t*** grid;
*grid = (grid_t**)malloc(sizeof(grid_t)*GRID_HEIGHT);
for(int i = 0 ; i < GRID_HEIGHT ; i++)
{
*grid[i] = (grid_t*)malloc(sizeof(grid_t)*GRID_WIDTH);
}