By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

link list -is this enough?

P: n/a
Hi,
I have a single link list. at the end of the program or in a
loop i want to destroy this link list.
so is it enough to free the pointer to this link list
or do i have to traverse whole link list until last node where next
field is null and free each node.
please suggest.
thanks
lee
--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Yang Lee wrote:
Hi,
I have a single link list. at the end of the program or in a
loop i want to destroy this link list.
so is it enough to free the pointer to this link list
or do i have to traverse whole link list until last node where next
field is null and free each node.
please suggest.


Lee...

If each list element was allocated using malloc() or calloc(),
then each element will need to be individually free()'d.

If storage for the entire list was allocated with a single
malloc() or calloc() call, then you'll need to use just one
free() call.

If the storage for the list wasn't allocated using malloc() or
calloc(), then you'd best not free() it.

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #2

P: n/a
> so is it enough to free the pointer to this link list
or do i have to traverse whole link list until last node where next
field is null and free each node.


If you free the pointer to the linked list your program will experience
a memory leak, so you should traverse the list and at each node record
the next pointer and then free the current node. Repeat this for all
nodes in the list.

Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.