开发者

Why my program have errors with memory leak checking?

When I use this

valgrind --leak-check=yes ./Main

I have about 185236 errors. It said that:

xx bytes in x blocks are possibly lost in loss record xxxx of xxxx

Here's my code:

Node InsertString(Head head, Node tree, char* data) {
    if (tree == NULL) {
        tree = ma开发者_Python百科lloc(sizeof (struct TreeNode)); //Error

        if (tree == NULL) {
            printf("Out of Space!\n");
        } else {
             tree->theData = malloc(sizeof (char) * strlen(data));//Error
            strcpy(tree->theData, data);
        }
    } else {
            if (strcmp(data, tree->theData) < 0) {
                tree->Left = InsertString(head, tree->Left, data); //Error
            } else {
                if (strcmp(data, tree->theData) > 0) {
                    tree->Right = InsertString(head, tree->Right, data);//Error
                }
            }
        } 

    }
    return tree;
}

Thank you!


Do you ever call free to deallocate the memory you are allocating with malloc?

If not, well, then you're leaking all that memory.


Use a tool called valgrind. It will tell you of such memory leaks.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜