Re: [NEWBIE] Chopping off linked lists

From: Bradley D. Hartin (bhartin@MA909A.TAMUCC.EDU)
Date: 04/20/98

On Mon, 20 Apr 1998, Mark Gerritsen wrote:

> j->next = NULL;

Nope, and here's why.

> or do you have to loop through the list and do a REMOVE_FROM_LIST for every
> entry with the field set to 0? If so, would it work like this?
>   for (j = list; j; j = j->next) {
>     if (j->field == 0)
>       REMOVE_FROM_LIST(j, list, next);

+       free(j);

>   }

If you simply do a 'j->next = NULL' then the memory used by j->next and
it's part of the list remains allocated.  So you will continue to leave
behind unrecoverable memory every time you do this.  Some compilers do
'garbage collection' where it will automatically free this space, but very
few do (last time I checked gcc doesn't).

Would any of the true C guru's here mind checking what I've said?  It's
been a while since I've messed with the nitty-gritty details of linked
lists, and when I did it was in C++ =P

Brad Hartin, Realms of Fury

     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | |

This archive was generated by hypermail 2b30 : 12/15/00 PST