link list -is this enough?

Y

Yang Lee

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
 
M

Morris Dovey

Yang said:
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.
 
R

Ravindranath Gummadidala

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top