Archived

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

KalvinB

clean up for linked classes

Recommended Posts

KalvinB    102
I''ve created a really basic demo for created essentially a linked list of classes (I''m sure there''s some sort of technical term for it) in C++ and it creates and displays the list properly but I''m not sure if the memory is being cleared right.
  
void an_organization::clear_group()
{
	for(group=tail;group->prev!=NULL; group=group->prev)
		group->next=NULL;
}
  
In Java all you have to do is leave the class unreferenced by anything which is basically what the above code is supposed to do. It starts at the end of the list and works it''s way backward disconnecting each node as it goes. Am I missing something, or that all that needs to be done? I''m thinking I need to add in a group->next->prev=NULL; before group->next=NULL; Ben [The Rabbit Hole | The Labyrinth | Programming | Gang Wars | The Wall]

Share this post


Link to post
Share on other sites
Oluseyi    2110
quote:
Original post by KalvinB
I''m thinking I need to add in a group->next->prev=NULL; before group->next=NULL;

1. If those are both pointers, don''t you think it might erase the existing object (group)? Check the values in the Debug window.

2. If the memory for the objects (nodes) in the list are dynamically allocated via new, you have to explicitly call delete on them.

3. Unless you have a specific reason to do so (such as learning purposes or special needs), I would recommend you look into std::list.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
KalvinB    102
1. No idea
2. They are allocated with new so I''ll get them removed with delete.
3. This is just a learning project. I''m taking a Java class so I''m learning the C++ equivilent on the side. Never had a real reason to use this stuff before.

It''s actually much easier than I remember it from my HS C++ class.

Ben

[The Rabbit Hole | The Labyrinth | Programming | Gang Wars | The Wall]

Share this post


Link to post
Share on other sites
Oluseyi    2110
quote:
Original post by KalvinB
When I run that it still shows the memory addresses for each member of the group. What am I doing wrong here?

This time you delete the contents of the memory location, but you don''t clear the memory location. You have to delete and set to NULL. (NOTE: If you set to NULL, trying to access a data member may result in an access violation).

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites