简体   繁体   中英

C++ error: Pointer being freed was not allocated

I'm building an AVL tree. I have a method to delete items in the tree, but I'm getting an error.

This is the run-time error I get:

malloc: *** error for object 0x100100120: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
jim Abort trap

My class looks like this:

struct Avlnode{
string data;
int balfact;
Avlnode *left;
Avlnode *right;
};

class Avltree{
public:
Avltree();
~Avltree( );
Avlnode* insert(string i, bool* j);
static Avlnode* buildtree ( Avlnode *root, string data, bool *h ) ;
void display( Avlnode *root );
Avlnode* deldata ( Avlnode* root, string data, bool *h );
static Avlnode* del ( Avlnode *node, Avlnode* root, bool *h );
static Avlnode* balright ( Avlnode *root, bool *h );
static Avlnode* balleft ( Avlnode* root, bool *h );
void setroot ( Avlnode *avl );
static void deltree ( Avlnode *root );
private:
Avlnode *root;
int items;
};

and the deldata is defined like this:

Avlnode* Avltree::deldata ( Avlnode *root, string data, bool *h ){
Avlnode *node;

if ( root == NULL ){
    //cout << "\nNo such data.";
    return ( root );
}
else{
    if ( data < root -> data ){
        root -> left = deldata ( root -> left, data, h ) ;
        if ( *h )
            root = balright ( root, h ) ;
    }
    else{
        if ( data > root -> data ){
            root -> right = deldata ( root -> right, data, h ) ;
            if ( *h )
                root = balleft ( root, h );
        }
        else{
            node = root;
            if ( node -> right == NULL ){
                root = node -> left ;
                *h = true ;
                delete ( node ) ;
            }
            else{
                if ( node -> left == NULL ){
                    root = node -> right ;
                    *h = true ;
                    delete ( node ) ;
                }
                else{
                    node -> right = del ( node -> right, node, h ) ;
                    if ( *h )
                        root = balleft ( root, h ) ;
                }
            }
        }
    }
}
return ( root ) ;
}

Avlnode* Avltree :: del ( Avlnode *succ, Avlnode *node, bool *h ){
Avlnode *temp = succ ;

if ( succ -> left != NULL ){
    succ -> left = del ( succ -> left, node, h ) ;
    if ( *h )
        succ = balright ( succ, h ) ;
}
else{
    temp = succ ;
    node -> data = succ -> data ;
    succ = succ -> right ;
    delete ( temp ) ;
    *h = true ;
}
return ( succ ) ;
}

Why am I getting this error?

tl;dr but - class managing memory + memory management errors ->

You're implementing a destructor, which means your copy/destroy logic has more to it than a shallow-copy can handle. Which makes sense, since you have a member Avlnode *root; .

Either use RAII, or properly implement a copy constructor and assignment operator.

This is known as the rule of three. All terms used are easily googleable.

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