以下代码是否存在内存泄漏

发布于 2024-12-04 14:14:23 字数 1110 浏览 0 评论 0原文

代码如下: 在将指针分配给保存类似节点指针的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福丶如此 2024-12-11 14:14:23

你没有内存泄漏。但为什么要释放内存呢?您将 btreeRight 存储在 root 中。

也许更好的缩进不会出错。

You do not have a memory leak. But why are you freeing the memory? You are storing the btreeRight in the root.

Perhaps better indentation would not go amiss though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文