如何使Valgrind记录所有分配
How to make Valgrind log all allocations?
我想让Valgrind记录分配,即使没有发现内存错误。如何做到这一点?
您可以使用Massif(一个valgrind工具)。手册链接很容易理解,但为了将来参考,这里是如何使用它,直接从手册:
valgrind --tool=massif prog
这将生成一个您可以使用ms_print
分析的文件。文件名为massif.out.<numbers>
。只需使用ms_print
即可获得良好的输出:
ms_print massif.out.12345
您要查找的内容可以在ms_print
输出的末尾找到。对于这个示例程序(手册中显示的程序):
#include <stdlib.h>
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%)
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 将两个数组中的差异记录在第三个数组中
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 我可以重新分配/覆盖std::字符串吗
- 在c++中使用动态分配的问题
- C++分配 - 动态分配数组并从文件中读取记录 - 挂起
- 如何使Valgrind记录所有分配