Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / C++ deleting trees question....

Author
Message
Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 19th Jun 2012 00:44 Edited at: 19th Jun 2012 00:45
This structure has some nesting can happen:

if I make a variable:
SYSMENUITEM SM;
and do this:
SM.Next=new SYSMENUITEM;
SM.Branch=new SYSMENUITEM;

then I delete the original:
delete SM;

Will the destructor ~SYSMENUITEM() destroy all the children automatically?

The fastest code is the code never written.
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 19th Jun 2012 01:20
Yes, that would work. There is a way to make the code a bit cleaner, and that's through using a smart pointer like std::unique_ptr.

Example using std::unique_ptr:

to use it:


Quote: "if I make a variable:
SYSMENUITEM SM;
and do this:
SM.Next=new SYSMENUITEM;
SM.Branch=new SYSMENUITEM;

then I delete the original:
delete SM;"

That might not work, because you can't 'delete' a regular variable. You'd have to let it go out of scope.

P.S. I'm a bit rushed for time, so some of this might not work or not make sense.

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 19th Jun 2012 01:38
unique_ptr is not a member of std

I looked through the std and couldn't find anything that looked like that......

The fastest code is the code never written.
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 19th Jun 2012 04:53
Which Visual Studio version are you using? If it's 2008, you might have to use auto_ptr instead. Or you could use Boost's smart pointer implementation that works the same way(shared_ptr or scoped_ptr).

Same principle applies, except std::auto_ptr can't be stored in STL containers(vector,deque,map,etc), unlike unique_ptr.

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 19th Jun 2012 05:02
2008.

So I can't use std::auto_ptr within a vector? OK. My original method will work if I change it to:

SYSMENUITEM *SM;
SM=new SYSMENUITEM;
and do this:
SM->Next=new SYSMENUITEM;
SM->Branch=new SYSMENUITEM;

then I delete the original:
delete SM;

The fastest code is the code never written.
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 19th Jun 2012 05:32
Not quite. More like this(if you really want the root to be a pointer):
SYSMENUITEM* root;
root->Next.reset(new SYSMENUITEM());
root->Branch.reset(new SYSMENUITEM());
access them like this..
root->Next->(data or whatever) = 0x0123;

delete root; // also deletes all branches and next in tree.

Another thing about auto_ptr is that it doesn't handle arrays properly and causes memory leaks.

Hawkblood
14
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 20th Jun 2012 03:01
I have a new solution I'm sure will work:

declaration:

SYSTEMMENU MS;
SYSMENUITEM tmp;
SM.Root.push_back(tmp);
.... set info for SM.Root[0]
from this point, I can create more "roots" by using ...Root[].Next.push_back(..) and branch by Root[].Branch.push_back(..)

It sounds confusing (because it is), but if I do this, when I want to delete the entire tree I could call:
SM.Root.clear();


Right???

The fastest code is the code never written.
Dar13
16
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 20th Jun 2012 03:20
That looks like it would work, can't test right now.

Login to post a reply

Server time is: 2024-11-19 00:24:21
Your offset time is: 2024-11-19 00:24:21