为什么Visual Studio CRT内存报告会显示CRT块

4

我正在使用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个字节的泄露常规块,这是我预料到的。但是一个CRT块中的4096字节是什么?根据微软的说法:
CRT块是由CRT库为自身使用分配的。 CRT库处理对这些块的释放。因此,除非发生了显着的错误(例如,CRT库已损坏),否则您不太可能在内存泄漏报告中看到这些内容。
我应该忽略这4096字节吗?谢谢。
2个回答

2

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

这个缓冲区在CRT正常终止时被释放。


-1

是的,CRT块可以安全地忽略。分配并不是由您的代码完成的,因此您不需要担心它


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接