메모리 누수를 찾기 위해 CRT 라이브러리를 사용하고 있습니다. 사실을 발견Visual Studio CRT 메모리 보고서에 CRT 블록이 표시되는 이유
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 바이트 일반 블록을 유출 :
#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;
}
나는 다음과 같은 출력을 얻을 :이 같은 일부 코드를 썼다. 나는 그것을 기대했다. 그러나 1 CRT 블록에서이 4096 바이트는 무엇입니까? Microsoft에 따르면 :
CRT 블록은 자체 용도로 CRT 라이브러리에 의해 할당됩니다. CRT 라이브러리는 이러한 블록에 대한 할당 해제를 처리합니다. 따라서 CRT 라이브러리가 손상된 것과 같이 잘못된 것이 아닌 한 메모리 누출 보고서에 이러한 오류가 표시되는 경우는 거의 없습니다.
그냥이 4096 바이트를 무시해야합니까? 감사.
감사합니다. 나는 그것을 upvote하는 데 충분한 명성이 없습니다. – Amber