gpt4 book ai didi

c - 大小 8 C + valgrind 的读取无效

转载 作者:行者123 更新时间:2023-12-01 23:39:24 30 4
gpt4 key购买 nike

Valgrind 正在输出这个

==7558== Invalid read of size 8
==7558== at 0x109049: delete_all_employees (emp.c:300)
==7558== by 0x108BCB: main (rpt.c:37)
==7558== Address 0x5232760 is 0 bytes inside a block of size
40 free'd
==7558== at 0x4C30D3B: free (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7558== by 0x108E15: free_emp (emp.c:175)
==7558== by 0x109044: delete_all_employees (emp.c:299)
==7558== by 0x108BCB: main (rpt.c:37)
==7558== Block was alloc'd at
==7558== at 0x4C31B25: calloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7558== by 0x108DAE: new_emp (emp.c:143)
==7558== by 0x108C73: add_employee (emp.c:65)
==7558== by 0x108FB9: load_employees (emp.c:272)
==7558== by 0x108BA2: main (rpt.c:28)

它调用错误的函数是这个

void delete_all_employees( void ) {
Employee *emp = employees;

while( emp ) {
free_emp(emp);
emp = emp->next;

}
}

我不知道为什么这会给我一个无效的读取大小 8

最佳答案

您释放了emp然后尝试从中读取内容。这就是 valgrind 所提示的。

在递增 emp 之前,将要释放的指针保存在单独的变量中,然后释放临时值。

while( emp ) {
Employee *tmp = emp;
emp = emp->next;
free_emp(tmp);
}

关于c - 大小 8 C + valgrind 的读取无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52671398/

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