Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


pointers, link list


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 oveja-carnivora   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 04:18 AM

Hi, I am using a link list in VC++ 6 for a project Im working on. How do I make sure that I have cleaned up the info in the link list? Will just setting all the pointers in the list to NULL work? how do I actualy clear the memory that it is using? will t automaticly clear it when I exit the function that they were declared in? thanxs

Sponsor:

#2 Rube   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 04:23 AM

Did you create the "nodes" with NEW? If so, you''ll need to call delete on them for every time you used NEW to free up the memory you used.

Setting a pointer to NULL doesn''t delete the memory, it only removes your pointer(s) to that memory making it in accessible.

You''ll have to walk through your linked list and call delete on each node.

R.

#3 oveja-carnivora   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 04:36 AM

so if I go thru the entire link list setting *currentPtr to NULL then that wil take care of it?

EDIT: would I also need to set currentPtr to NULL?

Edited by - oveja-carnivora on January 14, 2002 11:37:30 AM

#4 Fruny   Moderators   -  Reputation: 1653

Like
Likes
Like

Posted 14 January 2002 - 04:54 AM

No, just setting the pointers to NULL doesn''t cut it (you don''t have garbage collection in C/C++ ... do you come from the Java world ? ).

You have to call delete (C++) or free © on every pointer you have allocated. Then, you can set them to NULL. It''s safer, unless you know you''re going to discard the structure containing the pointer itself, in which case it doesn''t matter.

#5 Monolith   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 05:17 AM

A good way to delete an entire linked list would be to use a recursive algorithm..


void del_list(node *currentNode) {
if (currentNode->nextNode != NULL)
del_list(currentNode->nextNode);

delete currentNode->nextNode;
currentNode->nextNode = NULL;
}


.. something like that.

#6 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 14 January 2002 - 05:41 AM


  
ListType * pDel;
for( ListType * pTemp = pHeadOfList; pTemp; )
{
pDel = pTemp;
pTemp = pTemp->pNext;

if( pDel )
{ delete pDel; // Unless you used malloc, then free() is correct.

pDel = NULL;
}
}

That should work for ya.


#7 oveja-carnivora   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 05:46 AM

thanks

#8 oveja-carnivora   Members   -  Reputation: 122

Like
Likes
Like

Posted 14 January 2002 - 05:57 AM

Annoymous, you said that delete would work unless I used memalloc. I was working from MSDN and I did use malloc, thats the only way I could find in there to make a link list. how else could I do it?

#9 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 14 January 2002 - 06:03 AM

quote:
Original post by oveja-carnivora
Annoymous, you said that delete would work unless I used memalloc. I was working from MSDN and I did use malloc, thats the only way I could find in there to make a link list. how else could I do it?


Just use the free function instead of delete if you used malloc.

If you want to know how else to allocate memory, look up the ''new'' keyword.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS