Archived

This topic is now archived and is closed to further replies.

GekkoCube

deleting nested pointers.

Recommended Posts

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

Share this post


Link to post
Share on other sites
owl    376
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.

Share this post


Link to post
Share on other sites
GekkoCube    116
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?

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites
GekkoCube    116
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--;
}

Share this post


Link to post
Share on other sites
SiCrane    11839
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.

Share this post


Link to post
Share on other sites