因为编译时无法确定维度,所以我试图动态分配一个二维结构数组。代码可以编译,但在访问元素时出现了错误访问。
// The struct
typedef struct
{
NSInteger numActors;
Actor *a1;
Actor *a2;
Actor *a3;
Actor *a4;
Actor *a5;
} GridNode;
// In interface
GridNode **grid;
// In init
NSInteger nx = inFrame.size.width / blockSize;
NSInteger ny = inFrame.size.height / blockSize;
grid = malloc(sizeof(GridNode) * nx * ny);
grid[10][20].numActors = 3; // EXC_BAD_ACCESS