deleting nested pointers.

Started by
5 comments, last by GekkoCube 20 years, 1 month ago
Hi, im a little rusty in C++, its been a while. Fortunately my question is a simple one i think... If i created memory, lets call it node, and this node has a pointer to another dynamically alloc. memory, lets call it subnode. then if i call delete on Node, which calls the Node''s destructor, will the subnode''s destructor also be called or must I explicityl call delete on it as well? basically i have a template linked list class, and im using it in a program which uses the linkedlist with pointers as well as non-pointer created objects. thanks.
Advertisement
You should take care of the "sub-nodes" deletion by yourself. By deleting them on the parent-node destructor or by any other proper mean.
[size="2"]I like the Walrus best.
thanks.

that makes sense.
but the reason why i ask is this:

if my linked list class is a template class, how do i know HOW to delete the data within each node (deleting the nodes itself is easy).

For example, i created two linkedlists of type CObject and of type CObject*. Deleting the list of type CObject* is slightly more complicated since I need to call delete inside each node''s destruction.

should i have a DeletePtr() method for cases where the template data is indeed a ptr?
One option is to either make it a policy that the user of the class must deleted the pointers before deleting the container. This is the method that the containers in the standard library use.

Another option is to make your templated class store smart pointers such as boost::shared_ptr, and simply don''t instantiated your template with bare pointer types.

Another option is to do a partial template specialization (provided your compiler groks it) and have the template class delete the stored pointer in it''s destructor.

I personally prefer the second choice in most circumstances.
thanks.!
i am stil having a problem.
i now have a function that specifically delete a node knowing that its contents has a pointer pointing to more data.

my program now crashes every time i call this funtion to delete a node from the list. It crashes specifically after i delete it. It crashes on operations post-node-deletion.

here is the deleteptr() source.

void RemovePtr(int index)      {         Node<T>* curr = m_pRoot;            Node<T>* prev = m_pRoot;            if (index < 0 || index >= m_iLength)            return;         for (int i=0; i<index; i++)         {            if (curr->next)            {                             prev = curr;               curr = curr->next;            }                     }         prev = curr->next;         curr->DeletePtr();         delete curr;         curr = 0;         m_iLength--;      }
In the main text of your post you say you posted the deleteptr function, but that looks like a RemovePtr() function (that happens to call DeletePtr()). Is that the function you intended to post?

In any case, I don''t see any glaring problems with the code. (There are things I don''t understand, like why you''re bothering with the prev variable.) But while I don''t see anything show-stopping wrong, I also can''t say that it''s correct either. The problem could be an interaction with the way DeletePtr() or the node destructor is defined. Try posting a minimal, but complete source example that exhibits the crash.

This topic is closed to new replies.

Advertisement