【发布时间】:2015-10-01 10:41:22
【问题描述】:
这是我的程序
#include <vld.h>
using namespace std;
int main() {
int* p = new int(100);
}
视觉检漏报告
Visual Leak Detector Version 2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00891B60: 4 bytes ----------
Call Stack:
c:\xxx\documents\visual studio 2010\projects\stl1\stl1\stl1.cpp (11): stl1.exe!main + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): stl1.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): stl1.exe!mainCRTStartup
0x76B7338A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x774B97F2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x774B97C5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
64 00 00 00 d....... ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
The program '[8992] stl1.exe: Native' has exited with code 0 (0x0).
为什么是40 bytes 内存泄漏,真的应该是4 bytes。
谁能解释这里发生了什么?
【问题讨论】:
-
分配的内存块总是比最初请求的要大。
-
@Robinson int* 是 int(100);而不是 int[100]。
-
想想
X* p = new X[y],当你调用delete[] p时,所有的y析构函数都会被调用。但是,delete 只得到一个指针。它如何知道数组有多长?因为它存储在分配的块中,远远大于分配对象所需的内存。
标签: c++ memory-leaks visual-leak-detector