gpt4 book ai didi

c - 请帮我释放这个动态分配的数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:00:41 25 4
gpt4 key购买 nike

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() 的额外 block ed 最后,您一定遇到了段错误。

关于c - 请帮我释放这个动态分配的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19850997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com