I have following code snipped. Its not printing memory present in below program.
I ran in debug mode. To verify debug mode, I have added _DEBUG macro and its printing 20.
Can anyone please help here
Tool : Microsoft Visual Studio Professional 2022 (64-bit) - LTSC 17.4
我截取了以下代码。其打印内存不在下面的程序中。我在调试模式下运行。为了验证调试模式,我添加了_DEBUG宏和它的打印20。有人能在这里帮帮忙吗工具:Microsoft Visual Studio专业版2022(64位)-LTSC 17.4
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
#ifndef _DEBUG /* For RELEASE builds */
#define PI 3.14
#else /* For DEBUG builds */
#define PI 22
#endif
int main()
{
// int* ptr = new int[5];
int* ptr = (int*)malloc(sizeof(int) * 5);
for (int i = 0; i < 5; i++)
{
ptr[i] = (i+1) * 10;
}
for (int i = 0; i < 5; i++)
{
std::cout <<"\n"<< ptr[i];
}
std::cout << "\n" << PI;
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
_CrtDumpMemoryLeaks();
}
Output :
10
20
30
40
50
PI : 22
输出:10 20 30 40 50 PI:22
Hi,
I have following code snipped. Its not printing memory present in below program.
I ran in debug mode. To verify debug mode, I have added _DEBUG macro and its printing 20.
Can anyone please help here
嗨,我剪下了下面的代码。其打印内存不在下面的程序中。我在调试模式下运行。为了验证调试模式,我添加了_DEBUG宏并打印了20。
更多回答
You never check the return value from _CrtDumpMemoryLeaks
. It will most probably be 1
, meaning that there are memory leaks.
您从不检查_CrtDumpMemoyLeaks的返回值。它很可能是1,这意味着存在内存泄漏。
Look in the output window. Detected memory leaks! Dumping objects -> C:\dev\Test\Test.cpp(47) : {152} normal block at 0x00000253D9F66E90, 20 bytes long. Data: < ( > 0A 00 00 00 14 00 00 00 1E 00 00 00 28 00 00 00 Object dump complete.
查看输出窗口。检测到内存泄漏!正在转储对象->C:\Dev\Test\Test.cpp(47):位于0x00000253D9F66E90的{152}正常块,20字节长。数据:<(>0A 00 00 00 14 00 00 00 1E 00 00 00 28 00 00对象转储完成。
优秀答案推荐
我是一名优秀的程序员,十分优秀!