如何让Valgrind记录所有分配?

即使没有发现内存错误,我也想让Valgrind记录分配。 如何才能做到这一点?

你会使用Massif (valgrind工具)。 手动链接很容易遵循,但为了将来参考,这里是如何使用它,直接从手册:

valgrind --tool=massif prog 

这将生成一个可以使用ms_print进行分析的文件。 文件massif.out. 。 只需使用ms_print获得一个不错的输出:

 ms_print massif.out.12345 

您正在寻找的内容可以在ms_print的输出ms_print 。 对于此示例程序(它们在手册中显示的程序):

 #include  void g(void) { malloc(4000); } void f(void) { malloc(2000); g(); } int main(void) { int i; int* a[10]; for (i = 0; i < 10; i++) { a[i] = malloc(1000); } f(); g(); for (i = 0; i < 10; i++) { free(a[i]); } return 0; } 

我们可以看到谁分配了什么:

 ->79.81% (8,000B) 0x400589: g (in /home/filipe/dev/a.out) | ->39.90% (4,000B) 0x40059E: f (in /home/filipe/dev/a.out) | | ->39.90% (4,000B) 0x4005D7: main (in /home/filipe/dev/a.out) | | | ->39.90% (4,000B) 0x4005DC: main (in /home/filipe/dev/a.out) | ->19.95% (2,000B) 0x400599: f (in /home/filipe/dev/a.out) | ->19.95% (2,000B) 0x4005D7: main (in /home/filipe/dev/a.out) | ->00.00% (0B) in 1+ places, all below ms_print's threshold (01.00%)