我正在用C语言做一个小游戏:
将显示这样的板:
|
|||
|||||
|||||||
你必须拿着棍子,让人工智能挑最后一根棍子,让它输。为了让玩家能够选择棋盘的大小,我编写了以下函数:
char **disp_board(int size)
{
char **tab;
int i;
/*
* I malloc the board with the variable 'size' given by the user before the
* game starts
*/
if ((tab = malloc(sizeof(char *) * (size + 1))) == NULL)
return (NULL);
tab[size] = NULL;
i = 0;
while (i <= size)
{
if (i == 0)
{
if ((tab[i] = malloc(sizeof(char) + 1)) == NULL)
return (NULL);
tab[i] = my_strdup(my_strcat(tab[i], "|"));
i++;
}
if ((tab[i] = malloc(my_strlen(tab[i - 1]) + 3)) == NULL)
return (NULL);
tab[i] = my_strdup(my_strcat(tab[i - 1], "||"));
i++;
}
disp_board2(tab, size);
return (tab);
}
请注意
my_strdup
和my_strcat
与strdup
和strcat
完全相同。但问题就在这里!这个游戏经常运行,但有时(特别是当我选择10号和14号时),我会收到以下消息:
“***错误在./allum1:free():无效的下一个大小(fast):
0x00000000022953f0***allum1:malloc.c:2365:sysmalloc:Assertion
`(old_top==((mbinptr)(字符*)&((av)->存储箱[((1)-1)*2]))-
__内置_offsetof(struct malloc_chunk,fd))&&old_size==0)| |((无符号长)(old_size)>=(无符号长)(无符号长)
(struct malloc_chunk,fd_nextsize))+((2*(sizeof(size_t)))-1))&
~((2*(sizeof(size_t)))-1)&((old_top)—>size&0x1)&&
((unsigned long)old_end&pagemask)==0)失败。已中止“
所以我想问题出在我的马洛奇和自由,我再次检查,他们似乎都是合乎逻辑的!
提前谢谢你的帮助,我真的希望我能解决这个问题。
如果你对密码有任何疑问,请告诉我。
最佳答案
if ((tab[i] = malloc(sizeof(char) + 1)) == NULL)
return (NULL);
tab[i] = my_strdup(my_strcat(tab[i], "|"));
当您从
malloc
返回一个指针时,它不会指向任何特定的、当然不是可以连接某些内容的有效字符串。将tab[i]
传递给my_strcat
(假设它是一个连接函数)在tab[i]
包含一些正常内容之前没有意义。关于c - './a.out'中的错误:free():无效***`./allum1'中的错误:free():下一尺寸无效(快速):0x00000000023e13f0 ***,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28539404/