gpt4 book ai didi

c - Valgrind 报告太多 malloc

转载 作者:太空狗 更新时间:2023-10-29 15:26:18 29 4
gpt4 key购买 nike

考虑这段代码:

int main(int argc, char const *argv[])
{
char *string = NULL;
string = malloc(sizeof(char) * 30);
free(string);
return 0;
}

我 malloc 一个 char 指针,然后释放它。现在考虑 valgrind 输出:

==58317== Memcheck, a memory error detector
==58317== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==58317== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==58317== Command: ./a.out
==58317==
==58317==
==58317== HEAP SUMMARY:
==58317== in use at exit: 34,941 bytes in 424 blocks
==58317== total heap usage: 505 allocs, 81 frees, 41,099 bytes allocated
==58317==
==58317== LEAK SUMMARY:
==58317== definitely lost: 0 bytes in 0 blocks
==58317== indirectly lost: 0 bytes in 0 blocks
==58317== possibly lost: 0 bytes in 0 blocks
==58317== still reachable: 0 bytes in 0 blocks
==58317== suppressed: 34,941 bytes in 424 blocks
==58317==
==58317== For counts of detected and suppressed errors, rerun with: -v
==58317== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

怎么可能有那么多 malloc 和 free?

编辑:这是我使用 valgrind --leak-check=yes --gen-suppressions=all ./a.out 运行时得到的结果,我正在尝试制作一个支持文件.

==60943== Memcheck, a memory error detector
==60943== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==60943== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==60943== Command: ./a.out
==60943==
==60943==
==60943== HEAP SUMMARY:
==60943== in use at exit: 34,941 bytes in 424 blocks
==60943== total heap usage: 505 allocs, 81 frees, 41,099 bytes allocated
==60943==
==60943== LEAK SUMMARY:
==60943== definitely lost: 0 bytes in 0 blocks
==60943== indirectly lost: 0 bytes in 0 blocks
==60943== possibly lost: 0 bytes in 0 blocks
==60943== still reachable: 0 bytes in 0 blocks
==60943== suppressed: 34,941 bytes in 424 blocks
==60943==
==60943== For counts of detected and suppressed errors, rerun with: -v
==60943== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 15)

最佳答案

这些 block 是由链接到您的可执行文件的系统库分配的(其中一些也被释放)。

Valgrind 有一个默认值 suppression抑制系统库泄漏的文件,您可以在输出中进一步看到:

==58317==         suppressed: 34,941 bytes in 424 blocks

如果您想了解更多详细信息,可以使用 -v 选项。

关于c - Valgrind 报告太多 malloc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29495225/

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