Deleting linked lists.

Blair P. Houghton bhoughto at hopi.intel.com
Wed Mar 27 15:43:31 AEST 1991


In article <2636 at borg.cs.unc.edu> warner at weiss.cs.unc.edu (Byron Warner) writes:
>I was wondering, is it safe to unallocate a singly linked list like this:
>struct list *head; /* top of list */
>struct list *ptr = head;
>while (ptr){
>	free(ptr);
>	ptr = ptr->next;
>}

Nope.  Something will go wrong, and you can define the
limits of its wrongness, and you can define prevention
(I.e., if nothing _can_ go wrong, nothing will.)

Rest assured that halfway through that list someone will
fire up xtrek and while your program is swapped out the
system will notice that suddenly there's half a megabyte
you don't currently have malloc'ed...

				--Blair
				  "You obviously know how, so
				   I won't bore you with the
				   correct method, unless someone
				   wants to get into a STYLE WAR..."



More information about the Comp.lang.c mailing list