【发布时间】:2015-09-11 01:02:10
【问题描述】:
Valgrind 在下面的代码中给了我以下错误: 大小为 8 的无效写入:地址 XX 在大小为 33 的块内分配了 32 个字节
/*The function allocate memory and clean it for further use.*/
static void *ft_mal(size_t size)
{
void *ret;
char *tmp;
ret = malloc(size); /*LINE WITH ERROR*/
if (!ret)
return (NULL);
tmp = ret;
while (size) {
*tmp = 0;
tmp++;
size--;
}
return (ret);
}
我在下面的代码中使用了这个函数,在注释行中我也有两个 Invalid write of size 8 错误:
/*Splits a string into an array of strings*/
char **ft_splt(const char *s, char c)
{
char **ret;
char *str;
int i;
i = ct_wd(s, c);
ret = (char **)ft_mal(sizeof(*ret) * i + 1);
str = (char *)ft_mal(sizeof(*str) * ct_c(s, c) + i);
i = 0;
while (*s) {
while (*s && *s == c)
s++;
ret[i++] = (*s ? str : '\0'); /*LINE WITH ERROR*/
while (*s && *s != c)
*str++ = *s++;
if (*s)
*str++ = '\0';
}
ret[i] = 0; /*LINE WITH ERROR*/
return (ret);
}
我不明白为什么它会产生错误,因此也不知道如何解决它们。因此,当我释放 malloc 时出现错误。
如何解决这些无效的读/写错误?
编辑:
根据需要,我提供了更多代码。 ct_wd 和 ct_c 分别计算字符串参数中的单词数和字符数,以创建正确大小的 malloc。
static int ct_wd(const char *s, char c)
{
int nb;
nb = 0;
while (*s) {
while (*s && *s == c)
s++;
while (*s && *s != c)
s++;
nb = (*(s - 1) != c ? nb + 1 : nb);
}
return (nb);
}
static int ct_c(const char *s, char c)
{
int nb;
nb = 0;
while (*s) {
if (*s != c)
nb++;
s++;
}
return (nb);
}
这是一个 main.c 示例:
int main()
{
char s[] = "lut:les:enf:? ";
char **ret;
int i = 0;
ret = ft_splt(s, ':');
while (ret[i]) {
printf("[Resultat :]\n");
i++;
}
/* free all malloced pointer */
return (0);
}
编辑:解决方案
如下所述,有一个错误:
ret = (char **)ft_mal(sizeof(*ret) * i + 1);
应该是:
ret = (char **)ft_mal(sizeof(*ret) * (i + 1));
我在执行时仍然遇到段错误。似乎删除了这一行:
ret[i] = 0;
删除了 valgrind 错误和段错误。 我知道这条线并不是真正必要的,因为 ft_mal 已经清理了内存,但我仍然不明白为什么这会在 valgrind 中产生写入错误并使我出现段错误。
【问题讨论】:
-
如果您希望人们帮助您调试此问题,请发布SSCCE。在计算要分配多少内存的例程中,您可能有一个错误。
-
特别是
ct_wd和ct_c做什么?如果您可以提供一些具有它们的虚拟实现和main()函数的东西,这将更容易调试。 -
你有没有可能以某种方式重新定义了
malloc()? -
看来
ft_mal()是malloc() + memset()的实现。 -
是的,这正是它的本质。我的代码是一个学校项目的一部分,其中禁止使用上述命名函数。