row = n + 1;
col = n + 1;
//used n+1 and i=-1 to avoid segmentation faults
board = malloc(row*sizeof(char *));
for(i=-1;i<row;i++)
{
board[i] = malloc(col*sizeof(char));
if(board[i] == NULL)
{
printf("Out of memory");
exit(EXIT_FAILURE);
}
}
for(i=-1; i < n+1; ++i)
{
free(board [i]);
}
free(board);
当我尝试在运行时释放此数组时,我的编译器发疯了,请解释一下,谢谢。
数组在C中不能具有负索引。
在该行: for(i = -1; i < row; i++)
我非常确定,这里有一个错误,在那里free
释放了一个额外的块,这些块最后没有被malloc()
编辑,并且您肯定遇到了段错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句