gpt4 book ai didi

c - 为什么 valgrind 将我的内存报告为 "definitely lost"?

转载 作者:行者123 更新时间:2023-12-04 23:45:45 27 4
gpt4 key购买 nike

考虑这段代码:

#include <stdlib.h>

int* alloc()
{
return malloc(250 * sizeof(int));
}

int main()
{
int i;
int *vars[3];
for(i = 0; i < 3; ++i) {
vars[i] = alloc();
}
}

Valgrind 输出:

$ valgrind --leak-check=full ./lala
==16775== Memcheck, a memory error detector
==16775== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==16775== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==16775== Command: ./lala
==16775==
==16775==
==16775== HEAP SUMMARY:
==16775== in use at exit: 3,000 bytes in 3 blocks
==16775== total heap usage: 3 allocs, 0 frees, 3,000 bytes allocated
==16775==
==16775== 3,000 bytes in 3 blocks are definitely lost in loss record 1 of 1
==16775== at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16775== by 0x4005B3: alloc (lala.c:5)
==16775== by 0x4005DF: main (lala.c:13)
==16775==
==16775== LEAK SUMMARY:
==16775== definitely lost: 3,000 bytes in 3 blocks
==16775== indirectly lost: 0 bytes in 0 blocks
==16775== possibly lost: 0 bytes in 0 blocks
==16775== still reachable: 0 bytes in 0 blocks
==16775== suppressed: 0 bytes in 0 blocks
==16775==
==16775== For counts of detected and suppressed errors, rerun with: -v
==16775== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

根据 Valgrind's manual :

If --leak-check is set appropriately, for each remaining block, Memcheck determines if the block is reachable from pointers within the root-set. The root-set consists of (a) general purpose registers of all threads, and (b) initialized, aligned, pointer-sized data words in accessible client memory, including stacks.



据我了解,由于 main() 仍然指向“肯定丢失”的内存。函数的堆栈,它们应该被归类为“仍然可以访问”,对吧?

如果没有,我如何配置 Valgrind 以尝试从 main 访问内存块的堆栈,以确定它们是否“仍然可以访问”?

编辑:

请不要告诉我 free main 末尾的指针,这不是我要问的。关于 Valgrind 术语中“仍然可以到达”和“绝对丢失”之间的区别,请参阅以下答案: https://stackoverflow.com/a/3857638/578749

最佳答案

main 的堆栈时,你的内存肯定会丢失。被销毁,即当它返回时。因此,解决方案是不返回。

#include <stdlib.h>
int main()
{
/* your code here */
exit(0);
}

返回 0 或 exit(0) 的行为或 main应该是等价的。

现在输出是:
==5035== Memcheck, a memory error detector
==5035== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==5035== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==5035== Command: ./a.out
==5035==
==5035==
==5035== HEAP SUMMARY:
==5035== in use at exit: 3,000 bytes in 3 blocks
==5035== total heap usage: 3 allocs, 0 frees, 3,000 bytes allocated
==5035==
==5035== LEAK SUMMARY:
==5035== definitely lost: 0 bytes in 0 blocks
==5035== indirectly lost: 0 bytes in 0 blocks
==5035== possibly lost: 0 bytes in 0 blocks
==5035== still reachable: 3,000 bytes in 3 blocks
==5035== suppressed: 0 bytes in 0 blocks
==5035== Reachable blocks (those to which a pointer was found) are not shown.
==5035== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==5035==
==5035== For counts of detected and suppressed errors, rerun with: -v
==5035== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

关于c - 为什么 valgrind 将我的内存报告为 "definitely lost"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31061326/

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