简体   繁体   English

二进制搜索树-插入时出现内存泄漏

[英]Binary Search Tree - Memory Leak at insertion

I am trying to create a binary search tree and insert a new node in an iterative way. 我正在尝试创建一个二进制搜索树并以迭代方式插入一个新节点。 It is all working well except I am getting a memory leak in this function. 一切正常,除非我在此功能中发生内存泄漏。

Valgrind says 7 blocks (I am adding 7 nodes) are missing. Valgrind说缺少7个块(我要添加7个节点)。 I couldn't see where my leak is. 我看不到泄漏在哪里。 I would appreciate another look at my code. 我希望再看看我的代码。

void bst_insert_node(bstree* bst, unsigned long phone, char *name) {
    bst_node* current = bst->root;

    bst_node* parent = NULL;

    bst_node* new = (bst_node *)malloc(sizeof(bst_node));
    new->phone  = phone;
    new->left   =   new->right  =   new->parent =   NULL;
    new->name = malloc(sizeof(char) * (strlen(name)+1));
    strncpy(new->name,name,(strlen(name)+1));

    while(current != NULL) {

        parent = current;

        if(phone < current->phone) {
            current = current -> left;
        }
        else if(phone > current->phone) {
            current = current -> right;
        } else {
            free(new);
            printf("Diese Nummer ist schon bekannt \n");
            return;
        }
    }

    new->parent = parent;

    if(parent == NULL) {
        bst->root = new;
    }
    else if(new->phone < parent->phone) {
        parent->left = new;
    }
    else {
        parent->right = new;
    }
}

Free methods: 免费方法:

void bst_free_subtree(bst_node* node) {
if (node == NULL) return;

bst_free_subtree(node->left);
bst_free_subtree(node->right);
printf("\n Deleting node: %lu \t %s", node->phone,node->name);
free(node);}

void bst_free_tree(bstree* bst) {
    if(bst != NULL && bst->root != NULL) {
        bst_free_subtree(bst->root);
        bst->root = NULL;
    }
}

As we all discussed in the comments, your memory leak is that you're not freeing the node->name strings that you have allocated. 正如我们在评论中讨论的那样,您的内存泄漏是您没有释放已分配的node->name字符串。 You need to add two more free s to your code: 您需要在代码中再添加两个free

  • in bst_insert_node, in the case where you can't insert the node, free(new->name) before you free(new) 在bst_insert_node中,如果无法插入节点,则在free(new->name)之前先free(new->name) free(new)
  • in bst_free_subtree, free(node->name) before you free(node) 在bst_free_subtree中, free(node->name)之前先free(node->name) free(node)

There is also an off-by-one error allocating space for a copied string, as in your answer. 正如您的答案一样,在为复制的字符串分配空间时也存在一个错误的错误。 It might be simplest to just new->name = strdup(name) instead, which will do both the allocate and the copy in one go. 取而代之的是new->name = strdup(name)可能是最简单的,它将一次性完成分配和复制。

As an aside, if these are phone numbers then I'd probably store them as strings, not integers (or libphonenumber if you want to go the whole hog, but C++ not C) and if there's a problem inserting one then it might be better to return the error to the calling code (eg return true if inserted, false if not) and let that raise errors rather than printing from this code. 顺便说一句 ,如果这些是电话号码,那么我可能 会将 它们存储为字符串,而不是整数 (如果您想整头猪,请输入libphonenumber ,而不是C ++,而不是C),如果插入一个电话有问题,那可能会更好将错误返回给调用代码(例如,如果插入则返回true,否则返回false),并引发错误,而不是从该代码打印出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM