我试图在Mac计算机上运行我的代码。它总是在Windows上工作正常(我使用的是Microsoft Visual Studio),但是当我在XCode中运行它时,以下代码有时会因EXC_BAD_ACCESS错误而崩溃。
board_t* board_initBoard() {
board_t* newBoard = (board_t*)malloc(sizeof(board_t));
newBoard->grid = (square_t**)malloc(11 * sizeof(square_t));
for(int i = 0; i < 11; i++)
newBoard->grid[i] = (square_t*)malloc(11 * sizeof(square_t));
for(int i = 0; i < 11; i++) {
for(int j = 0; j < 11; j++) {
if(i * j * (10 - i) * (10 - j) == 0 && (i + j) % 10 > 2 && (i + j) % 10 < 8)
newBoard->grid[i][j] = black;
else if(i * j * (10 - i) * (10 - j) == 225)
newBoard->grid[i][j] = black;
else if(i == 5 && j == 5)
newBoard->grid[i][j] = king;
else if(i * j * (10 - i) * (10 - j) >= 576)
newBoard->grid[i][j] = white;
else if((i == 5 && j * (10 - j) == 21) || (j == 5 && i * (10 - i) == 21))
newBoard->grid[i][j] = white;
else if(i * (10 - i) + j * (10 - j) == 0)
newBoard->grid[i][j] = hostile;
else
newBoard->grid[i][j] = empty;
}
}
return newBoard;
}
错误仅发生在大约1/3次,并且总是发生在第二个for
循环中,但是在随机点。可能导致这种情况发生的原因是什么?