在工作中,我正在用 C 编写一个相当复杂的软件,我经常使用 valgrind 对其进行测试。该程序到目前为止运行完美,没有内存泄漏或数组边界违规,并且在 valgrind 报告中,“frees”的数量与“mallocs”的数量相匹配 - 太棒了。让我烦恼的是它报告了数以千计的释放和 malloc。而且我知道我做的不超过 50-60 个事实。我确实知道,当我的程序调用“fopen”时,valgrind 会将该调用计入 malloc 的数量,同样,“fclose”也会计入“释放”的数量。但就我而言,这仍然不能解释我看到的内存分配和释放次数的数字。我仔细搜索了我的代码以寻找罪魁祸首,但我一无所获。由于显而易见的原因,我不能在这里发布任何代码,但我只是想知道,我是否遗漏了什么? valgrind 是否将其他 C 操作计入 malloc 和 frees 的数量?
这是我的 valgrind 报告。如您所见,从这个角度看一切都很好。
Memcheck, a memory error detector
Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
Command: ./Codec
Parent PID: 3526
HEAP SUMMARY:
in use at exit: 0 bytes in 0 blocks
total heap usage: 2,407 allocs, 2,407 frees, 28,877,748 bytes allocated
All heap blocks were freed -- no leaks are possible
For counts of detected and suppressed errors, rerun with: -v
ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 6)
最佳答案
好吧,如果您调用执行 malloc 和 free 调用的库函数,您将看到很多分配和释放。一些库函数,进行分配的系统调用是 strdup、pthread_create、timer_create、e.t.c
关于c - valgrind 将哪些所有 C 操作视为 'malloc' 和 'free' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19305224/