1

I am trying to create an AVL tree and i am using a dynamic array of pointers to track which nodes are affected by the insertion.My Question is if the deletion of the dynamic array will delete the content of the pointers as well.The code is not finished but i think it will give you an idea of what i mean.If it does not work then how should i do it?Thanks in advance.

treenode *newnode,**roadnodes,*parent ;
int x=(int)log(numberofnodes)+2,l=0 ;
bool *rightorleft,flag=true ;
newnode=new treenode ;
roadnodes=new treenode*[x] ;
rightorleft=new bool[x] ;
newnode->id=i ;
newnode->hl=0;
newnode->hr=0;
newnode->hm=0;
newnode->left=NULL;
newnode->right=NULL ;
if(head==NULL)
{
    delete[] roadnodes ;
    delete[] rightorleft ;
    numberofnodes++ ;
    head=newnode ;
    return true ;
}
parent=head ;
while(flag)
{
    roadnodes[l]=parent ;
    if(parent->id>i)
    {
        if(parent->left)
            parent=parent->left ;
        else
        {
            flag=false ;
            parent->left=newnode ;
        }
        rightorleft[l]=true ;
        l++ ;
    }
    else
    {
        if(parent->right)
            parent=parent->right ;
        else
        {
            flag=false ;
            parent->right=newnode ;
        }
        rightorleft[l]=false ;
        l++ ;
    }

}
return true ;
4
  • 3
    This problem just goes away if you use smart pointers instead of manual memory management.. Commented May 10, 2017 at 17:30
  • 3
    std::unique_ptr<treenode> and std::vector<std::unique_ptr<treenode>> to avoid any leaks. Commented May 10, 2017 at 17:30
  • 3
    rightorleft[l]=true is like those annoying people who'll answer "yes" when you ask "coffee or tea?". Use enum Direction { left, right }; or something like that. Commented May 10, 2017 at 17:32
  • What do you mean by, "delete the content of the pointers"? Are you asking if deleting the dynamic array will call delete on each pointer in it? Commented May 10, 2017 at 18:02

2 Answers 2

1

If you are asking whether delete[] roadnodes; will implicitly delete roadnodes[0]; delete roadnodes[1]; ..., the answer is no. It absolutely will not. You are responsible for deleting them. Using standard containers and smart pointers, as suggested in a couple of comments, is a much better way to go.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot man.I actually want to keep the contents but delete the pointers.So i guess i am ok in this situation?
The terminology "keep the contents" or "delete the contents" is sufficiently confusing that I'm not going to attempt to answer further.
1

Question is if the deletion of the dynamic array will delete the content of the pointers as well.

When you call delete on dynamic array then destructor of each element is called. As you may guess raw pointers do not have destructors or it is a noop so no, that memory would not be released. So you either use smart pointers which destructor would release memory or you need to call delete for each pointer manually.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.