简体   繁体   中英

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. 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 .

Perhaps better indentation would not go amiss though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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