以下代码是否会泄漏内存

时间:2011-09-12 20:13:35

标签: c memory-management memory-leaks

代码如下: 在将指针指向保存类似节点指针的btreeRight后,我释放root->pointers[0]。释放后,我的程序不稳定,无法获得平衡的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;
      }

1 个答案:

答案 0 :(得分:4)

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

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