【发布时间】:2010-12-08 20:50:00
【问题描述】:
这些天我正在处理 C 代码中的内存泄漏。我很惊讶地看到不同硬件架构中的不同内存泄漏。
以下是 valgrind 在 64 位架构中展示的泄漏摘要:
LEAK SUMMARY:
==4064== definitely lost: 1,600,192 bytes in 7 blocks
==4064== indirectly lost: 0 bytes in 0 blocks
==4064== possibly lost: 0 bytes in 0 blocks
==4064== still reachable: 3,217,416 bytes in 2,155 blocks
==4064== suppressed: 0 bytes in 0 blocks
==4064== Reachable blocks (those to which a pointer was found) are not shown.
==4064== To see them, rerun with: --leak-check=full --show-reachable=yes
==4064==
==4064== For counts of detected and suppressed errors, rerun with: -v
==4064== ERROR SUMMARY: 7 errors from 7 contexts (suppressed: 4 from 4)
以下是32位架构
LEAK SUMMARY:
==29607== definitely lost: 1,600,048 bytes in 4 blocks .
==29607== indirectly lost: 456 bytes in 33 blocks.
==29607== possibly lost: 0 bytes in 0 blocks.
==29607== still reachable: 96 bytes in 12 blocks.
==29607== suppressed: 0 bytes in 0 blocks.
==29607== Reachable blocks (those to which a pointer was found) are not shown.
==29607== To see them, rerun with: --show-reachable=yes
这可能是什么原因。
【问题讨论】:
-
输出是正常退出的程序?如果它在失败时存在,它可能会在不同的地方失败。由于崩溃通常与内存管理有很大关系,它可能会在每个架构之前或之后崩溃一点,因此差异很大。另请注意,64 位通常比 32 位使用更多的内存(即指针大小等)
-
这是否意味着如果我能够设法防止任何架构中的内存泄漏,那么它将适用于两种架构?
-
正确的程序不会泄漏:)
标签: c memory memory-management valgrind