[英]Why is a call-stack array causing a memory leak?
我有一個小代碼段,要求用戶在運行valgrind --leak-check=full ./exercise1
時進行5次加倍操作,出現內存泄漏,說:
==6765==
==6765== HEAP SUMMARY:
==6765== in use at exit: 72,704 bytes in 1 blocks
==6765== total heap usage: 8 allocs, 7 frees, 75,037 bytes allocated
==6765==
==6765== LEAK SUMMARY:
==6765== definitely lost: 0 bytes in 0 blocks
==6765== indirectly lost: 0 bytes in 0 blocks
==6765== possibly lost: 0 bytes in 0 blocks
==6765== still reachable: 72,704 bytes in 1 blocks
==6765== suppressed: 0 bytes in 0 blocks
==6765== Reachable blocks (those to which a pointer was found) are not shown.
==6765== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==6765==
==6765== For counts of detected and suppressed errors, rerun with: -v
==6765== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我不確定為什么我的這段代碼會留下內存。 在代碼中的任何地方都沒有使用堆分配的數組,也沒有使用任何指針。 這意味着我將不需要使用任何delete []來刪除任何對象。 我很困惑為什么我還剩下內存。 這是我的代碼:
#include <iostream>
using namespace std;
int main(){
}
對於C ++實現,通常為內部使用分配內存。 不釋放用於程序全局狀態的動態內存也是一種常見的模式。 valgrind報告的“可訪問”內存與您的代碼無關,因為您的代碼未分配任何動態內存。
使用--show-reachable=yes
可以告訴您更多信息。 我將顯示程序的輸出
int main(){}
==10673== 72,704 bytes in 1 blocks are still reachable in loss record 1 of 1
==10673== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10673== by 0x4E78745: pool (eh_alloc.cc:123)
==10673== by 0x4E78745: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
==10673== by 0x4E78745: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
==10673== by 0x40106B9: call_init.part.0 (dl-init.c:72)
==10673== by 0x40107CA: call_init (dl-init.c:30)
==10673== by 0x40107CA: _dl_init (dl-init.c:120)
==10673== by 0x4000C69: ??? (in /lib/x86_64-linux-gnu/ld-2.23.so)
ld是Linux上的動態鏈接程序/加載程序庫。
PS double array[size];
由於size
不是編譯時常數表達式,因此格式不正確。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.