gpt4 book ai didi

c++ - 为什么 Visual Studio CRT 内存报告显示 CRT block

转载 作者:太空狗 更新时间:2023-10-29 23:15:41 47 4
gpt4 key购买 nike

我正在练习使用 CRT 库查找内存泄漏。我写了一些这样的代码:

#define _CRTDBG_MAP_ALLOC
#include <stdio.h>
#include <stdlib.h>
#include <crtdbg.h>

typedef struct NodeLL {
int value;
struct NodeLL *next;
} Node;

void printLL(Node *pHead) {
int i=0;
while(pHead) {
printf("%d\n", pHead->value);
i++;
pHead = pHead->next;
}
}

Node * addNode(Node *pHead, int value) {
Node *pNew, *pLL;
pNew = (Node *)malloc(sizeof(Node));
pNew->value = value;
pNew->next = NULL;
if(!pHead) {
pHead = pNew;
}
else {
pLL = pHead;
while(pLL->next)
pLL = pLL->next;
pLL->next = pNew;
}

return pHead;
}

void deleteNodes(Node *pHead) {
Node *pLL;
int i=0;
while(pHead) {
printf("deleting node %d, value is %d\n", i, pHead->value);
i++;
pLL = pHead->next;
free(pHead);
pHead = pLL;
}
}


Node * removeDups(Node *pHead) {
if (!pHead)
return NULL;
Node *pNode2, *pPrev;
Node *pNode = pHead;
while(pNode) {
pPrev = pNode;
pNode2 = pNode->next;
while(pNode2) {
if(pNode2->value == pNode->value) {
pPrev->next = pNode2->next;
free(pNode2);
pNode2 = pPrev->next;
}
else {
pPrev = pNode2;
pNode2 = pNode2->next;
}
}
pNode = pNode->next;
}
return pHead;
}

int main() {

_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
//_CrtSetBreakAlloc(71); // used to break at the second malloc

_CrtMemState s1, s2, s3;

// take a snap shot of memory before allocating memory
_CrtMemCheckpoint(&s1);

int NodeNum, i, j, value;
Node *pHead = NULL;

printf("How many nodes in the linked list?");
scanf("%d", &NodeNum);
for (i=0; i<NodeNum; i++) {
printf("Please enter Node %d value:", i);
scanf("%d", &value);
pHead = addNode(pHead, value);
}


printLL(pHead);
printf("remove duplicates\n");
pHead = removeDups(pHead);
printLL(pHead);
// clean up
//deleteNodes(pHead);

_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
_CrtDumpMemoryLeaks();


// take a snap shot of memory after allocating memory
_CrtMemCheckpoint(&s2);

if(_CrtMemDifference(&s3, &s1, &s2) )
_CrtMemDumpStatistics(&s3);

return 0;
}

我得到以下输出:

Detected memory leaks!

Dumping objects -> ...

\2_1_removedupll\removedupsll.cpp(23) : {72} normal block at 0x00701570, 8 bytes long.
Data: < > 03 00 00 00 00 00 00 00

\2_1_removedupll\removedupsll.cpp(23) : {71} normal block at 0x00701528, 8 bytes long.
Data: < p p > 02 00 00 00 70 15 70 00

\2_1_removedupll\removedupsll.cpp(23) : {70} normal block at 0x007014E0, 8 bytes long.
Data: < ( p > 01 00 00 00 28 15 70 00

Object dump complete.

0 bytes in 0 Free Blocks.

24 bytes in 3 Normal Blocks.

*4096 bytes in 1 CRT Blocks.*

0 bytes in 0 Ignore Blocks.

0 bytes in 0 Client Blocks.

Largest number used: 3870 bytes.

Total allocations: 4120 bytes.

它发现泄漏了 24 字节的正常 block 。我期望。但是 1 个 CRT block 中的 4096 字节是多少?据微软称:

CRT block 由 CRT 库分配供自己使用。 CRT 库处理这些 block 的释放。因此,您不太可能在内存泄漏报告中看到这些内容,除非出现严重错误,例如 CRT 库已损坏。

我应该忽略这 4096 字节吗?谢谢。

最佳答案

这 4,096 字节的分配是 stdout 的临时缓冲区,它是在您第一次调用 printf 时分配的。因为您第一次调用 printf 是在您的两个内存检查点之间,所以它显示为两个检查点之间的差异。如果您在第一个内存检查点之前添加对 printf 的调用,则这 4,096 字节的分配不会出现在差异中。

当 CRT 正常终止时,此缓冲区将被释放。

关于c++ - 为什么 Visual Studio CRT 内存报告显示 CRT block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28102835/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com