gpt4 book ai didi

c - 没有自由字符串的自由双数组字符

转载 作者:行者123 更新时间:2023-11-30 18:41:19 25 4
gpt4 key购买 nike

我正在寻找重新分配一个没有空闲字符串的双字符数组,这是我的代码:

char **rea_scd_array(char **src, int add)
{
char **ret;
int i;

i = 0;
while (src[i] != '\0')
i++;
ret = (char**) malloc(sizeof(char*) * (i + add + 1));
ret[i + add] = '\0';
while (i >= 0)
{
ret[i] = &src[i][0];
i--;
}
free(&src);
return (ret);
}

我的想法是添加新的维度来存储字符串,而不需要重新分配数组的所有表并复制所有旧内容。

我的代码不起作用,我明白了

`"malloc: *** error for object 0x7fff5fbff9e8: pointer being freed was not allocated
set a breakpoint in malloc_error_break to debug"`

我只是想释放存储指向字符串的指针的第二个数组维度。

有什么想法吗?

最佳答案

您首先尝试释放从未分配过内存的src。您可以释放动态分配给堆的内存(通常通过 malloc)。

关于c - 没有自由字符串的自由双数组字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22322991/

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