尺寸在編譯時是未知的,所以我試圖動態地分配一個二維的結構數組。代碼編譯但訪問元素時訪問錯誤。如何動態分配結構的二維數組?
// 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
nx和ny的值是什麼?你確定他們是你想要他們的嗎? – WildCrustacean 2010-06-22 14:59:57
當我測試時,nx和ny都是128。 – Morrowless 2010-06-22 15:01:08