gpt4 book ai didi

C valgrind内存泄漏

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

嗨,我的 Valgrind 显示内存错误,但我找不到它。

==18608== HEAP SUMMARY:

==18608== in use at exit: 88 bytes in 1 blocks

==18608== total heap usage: 4 allocs, 3 frees, 2,220 bytes allocated

==18608==

==18608== Searching for pointers to 1 not-freed blocks

==18608== Checked 53,708 bytes

==18608==

==18608== 88 bytes in 1 blocks are definitely lost in loss record 1 of 1

==18608== at 0x402D17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)

==18608== by 0x8 0485BF: pmalloc (in auf)

==18608== by 0x8048680: main (in auf)

==18608==

==18608== LEAK SUMMARY:

==18608== definitely lost: 88 bytes in 1 blocks

==18608== indirectly lost: 0 bytes in 0 blocks

==18608== possibly lost: 0 bytes in 0 blocks

==18608== still reachable: 0 bytes in 0 blocks

==18608== suppressed: 0 bytes in 0 blocks

==18608==

==18608== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

==18608== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

最佳答案

您分配了两个不同的int *;您不会释放第一个资源,但会释放第二个资源而不对其进行任何操作:

print_prim(pmalloc(n), laenge);
free(pmalloc(laenge));

要正确释放第一个资源,您可以执行以下操作:

int *p = pmalloc(n);
print_prim(p, laenge);
free(p);

关于C valgrind内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46961981/

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