clean up for linked classes

Started by
5 comments, last by KalvinB 22 years, 2 months ago
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]
Advertisement
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!
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]
Unless you''re taking the address of stuff on the stack and sticking that into the list (also a bad idea), that code is leaking.
MSVC should complain loudly about it (upon termination) in a debug build.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
  	for(group=tail;group->prev!=NULL; group=group->prev)		delete group;	for(group=tail;group->prev!=NULL; group=group->prev)		cout<<group<<endl;  


When I run that it still shows the memory addresses for each member of the group. What am I doing wrong here?

Ben

[The Rabbit Hole | The Labyrinth | Programming | Gang Wars | The Wall]
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!
Got it. Thanks.

Ben

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

This topic is closed to new replies.

Advertisement