Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!asuvax!noao!arizona!dave From: dave@cs.arizona.edu (Dave Schaumann) Newsgroups: comp.lang.c Subject: Re: Deleting linked lists. Keywords: C, C++. Message-ID: <1269@caslon.cs.arizona.edu> Date: 27 Mar 91 05:04:27 GMT References: <2636@borg.cs.unc.edu> Distribution: na Organization: U of Arizona CS Dept, Tucson Lines: 20 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; >} Who knows? Who cares, when it's so easy to get it right? struct list *head, *p, *q ; for( p = head ; p != NULL ; p = q ) { q = p->next ; free(p) } -- Dave Schaumann | dave@cs.arizona.edu | Short .sig's rule!