Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!cs.umn.edu!talon.UCS.ORST.EDU!usenet!ogicse!intelhf!ichips!inews!hopi!bhoughto From: bhoughto@hopi.intel.com (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: Deleting linked lists. Keywords: C, C++. Message-ID: <3483@inews.intel.com> Date: 27 Mar 91 05:43:31 GMT Article-I.D.: inews.3483 References: <2636@borg.cs.unc.edu> Sender: news@inews.intel.com Distribution: na Organization: Intel Corp, Chandler, AZ Lines: 23 In article <2636@borg.cs.unc.edu> warner@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..."