简体   繁体   English

以下代码是否会泄漏内存

[英]Does the following code leaks memory

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. 代码如下:我解放了btreeRight assiging的指针后root->pointers[0]持有类似node.After的释放它在我的计划是不稳定的,没有得到均衡的B树指针。 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;
      }

You do not have a memory leak. 您没有内存泄漏。 But why are you freeing the memory? 但是,为什么要释放内存? You are storing the btreeRight in the root . 您将btreeRight存储在root

Perhaps better indentation would not go amiss though. 也许更好的缩进不会错。

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

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