c++ - 为什么 valgrind 将 64 位程序中的指针大小报告为 4 个字节

标签 c++ valgrind

我编写了一个测试 C++ 程序来检查 valgrind 的输出。代码是

#include <iostream>

void f() {
    int *pp = new int(1);
    std::cout << "pp is " << *pp << "\n";
}

int main() {
    f();
    return 0;
}

我使用的valgrind命令是

valgrind --leak-check=yes ./a.out

Valgrind 的输出是

==2255== HEAP SUMMARY:
==2255==     in use at exit: 4 bytes in 1 blocks
==2255==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==2255== 
==2255== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1
==2255==    at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2255==    by 0x400786: f() (in /run/shm/a.out)
==2255==    by 0x4007CC: main (in /run/shm/a.out)
==2255== 
==2255== LEAK SUMMARY:
==2255==    definitely lost: 4 bytes in 1 blocks
==2255==    indirectly lost: 0 bytes in 0 blocks
==2255==      possibly lost: 0 bytes in 0 blocks
==2255==    still reachable: 0 bytes in 0 blocks
==2255==         suppressed: 0 bytes in 0 blocks

我使用的是 Ubuntu 机器: Linux Sun 3.2.0-27-generic#43-Ubuntu SMP Fri Jul 6 14:25:57 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux

gcc 版本“4.6.3”

gcc 参数我用了“-g -m64”

我想应该是八个字节吧?

最佳答案

不,它不应该是八个字节,除非你的平台上有 sizeof(int)==8

您正在分配和泄漏单个 int,而不是指针。

关于c++ - 为什么 valgrind 将 64 位程序中的指针大小报告为 4 个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12998787/

相关文章:

c++ - 更改 cv::Mat 图像的类型(解释)

c++ - 在 Visual Studio 中使用 C++ 类 - 未声明的标识符错误

c++ - 异或数据包中的所有数据

c - 安装中是否包含 valgrind 抑制文件?

valgrind - 我如何在不通过 valgrind 命令选项启动它的情况下对每个 Process 实例进行 valgrind memcheck

c++ - 您如何使用 clang 的新自定义大小 int 功能?

c++ - 这个 C++ 成员初始化行为是否定义良好?

c++ - callgrind 有合理的替代品吗?

c++ - Valgrind 和内存泄漏

macos - 安装 Valgrind 时 make 失败