【发布时间】:2019-03-01 20:17:29
【问题描述】:
我知道有很多人问这个问题,我已经查看了这些帖子上的所有答案,但仍然没有解决我的问题。 我正在尝试在 C 中释放二进制搜索树。我编写了用于释放内存的代码。下面是 Insert、createNode 和 freeNode 的代码:
插入
Node *insertNode(Node *root, int value) {
/*
Insertion of node in Binary Search Tree. The new node must be added in the correct subtree (either left or right).
If the value is less than the root value then it should be insert in the left subtree. If it's bigger then it should be
on the right.
*/
if (root == NULL) {
//if this is the first node then return its value.
root = createNode(value);
return root;
}
//on the left subtree
if (value < root->data) {
//recurse down the left subtree
root->left = insertNode(root->left, value);
} else if (value > root->data) {
//recurse down the right subtree otherwise.
root->right = insertNode(root->right, value);
}
return root;
}
免费树
void freeSubtree(Node *N) {
if(N == NULL) {
return;
} else{
freeSubtree(N->right);
freeSubtree(N->left);
N->right = NULL;
N->left = NULL;
}
free(N);
}
创建新节点
Node *createNode(int value) {
//allocate space for the node.
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
我不知道为什么我已经释放了所有节点后仍然存在内存泄漏。我看不出我哪里出错了。
任何帮助将不胜感激!
编辑 以下是 valgrind 报告的内存泄漏 Valgrind memory leak error
【问题讨论】:
-
你怎么知道你有内存泄漏?
-
那些对我来说看起来不错,使用眼球而不是编译器。可以进行各种小的改进,但我认为没有什么重要的。因此,问题变成了“您如何使用此代码?”请创建一个显示泄漏的 MCVE (minimal reproducible example)。
-
泄漏来自两个不同的调用链。帮自己一个忙,在你的(所有)构建中包含
-g——编译和链接行。然后您将在 Valgrind 输出中获得行号。如果你不使用-g,你就是在浪费 Valgrind。请显示您用于生成泄漏的代码(这是我之前要求的 MCVE)。我们不必猜测您正在执行的操作顺序。 -
我尝试了这样的
main()函数——使用的数字有几个变体,并且许多运行随机序列——并且从未泄漏任何内存:int main(void) { Node *root = 0; srand(time(0)); for (int i = 0; i < 20; i++) root = insertNode(root, rand() % 53); for (int i = 0; i < 20; i++) root = insertNode(root, (13 * i + 7) % 47); freeSubtree(root); return 0; }——现在至关重要的是,你准确地展示你在做什么。 -
PLo - 确保您使用
-g编译以生成符号(如果使用 gcc)并将valgrind输出作为文本发布在问题底部(正确缩进 4 个空格)。
标签: c memory malloc binary-search-tree free