Reputation: 73
I am practicing using CRT library to find memory leaks. I wrote some code like this:
#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;
}
I get the following output:
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.
It found leaked 24 bytes normal blocks. I expected that. But what is this 4096 bytes in 1 CRT Blocks? According to microsoft:
A CRT block is allocated by the CRT library for its own use. The CRT library handles the deallocation for these blocks. Therefore, it is unlikely you will see these in the memory leak report unless something is significantly wrong, for example, the CRT library is corrupted.
Should I just ignore this 4096 bytes? Thanks.
Upvotes: 4
Views: 1398
Reputation: 355069
This 4,096 byte allocation is the temporary buffer for stdout
, which is allocated when you first call printf
. Because you first call to printf
is between your two memory checkpoints, it appears as a difference between the two checkpoints. If you add a call to printf
before your first memory checkpoint, this 4,096 byte allocation will not appear in the difference.
This buffer is freed when the CRT terminates normally.
Upvotes: 2
Reputation: 1
Yes, the CRT blocks can be safely ignored. The allocation was not done by your code, so you needn't bother about it
Upvotes: -1