以下代码是否存在内存泄漏
代码如下: 在将指针分配给保存类似节点指针的 root->pointers[0]
后,我正在释放 btreeRight
。释放它后,我的程序不稳定并且不得到平衡的btree。您能否告诉我我是否正确释放了内存?如果没有,请建议我如何释放它?
int order =5;
typedef struct BTREE_HELP {
//
}*BTREE,BTREE_NODE;
BTREE btree_Start(void){
BTREE TempBtreeNode;
TempBtreeNode = malloc(sizeof(BTREE_NODE));
TempBtreeNode->keys=malloc((order-1) * sizeof(int));
TempBtreeNode->pointers=malloc((order) * sizeof(void *) );
TempBtreeNode->isLeaf = FALSE;
TempBtreeNode->numKeys = 0;
TempBtreeNode->parent = NULL;
TempBtreeNode->next = NULL;
return TempBtreeNode;
}
BTREE btree_NewRoot(int key,BTREE btreeRight) {
BTREE root;
root = btree_start();
root->keys[0] = key;
root->pointers[0] = btreeRight;
root->numKeys++;
root->parent = NULL;
btreeRight->parent = root;
free(btreeRight->keys);
free(btreeRight->pointers);
free(btreeRight);
return root;
}
The code is as follows:
I am freeing the btreeRight
after assiging the pointer to the root->pointers[0]
which hold the pointer of similar node.After freeing it my program is unstable and not getting the balanced btree. Could you please let me know if i am freeing the memory correctly? If not please suggest how would I free it?
int order =5;
typedef struct BTREE_HELP {
//
}*BTREE,BTREE_NODE;
BTREE btree_Start(void){
BTREE TempBtreeNode;
TempBtreeNode = malloc(sizeof(BTREE_NODE));
TempBtreeNode->keys=malloc((order-1) * sizeof(int));
TempBtreeNode->pointers=malloc((order) * sizeof(void *) );
TempBtreeNode->isLeaf = FALSE;
TempBtreeNode->numKeys = 0;
TempBtreeNode->parent = NULL;
TempBtreeNode->next = NULL;
return TempBtreeNode;
}
BTREE btree_NewRoot(int key,BTREE btreeRight) {
BTREE root;
root = btree_start();
root->keys[0] = key;
root->pointers[0] = btreeRight;
root->numKeys++;
root->parent = NULL;
btreeRight->parent = root;
free(btreeRight->keys);
free(btreeRight->pointers);
free(btreeRight);
return root;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你没有内存泄漏。但为什么要释放内存呢?您将
btreeRight
存储在root
中。也许更好的缩进不会出错。
You do not have a memory leak. But why are you freeing the memory? You are storing the
btreeRight
in theroot
.Perhaps better indentation would not go amiss though.