home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gatech!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Subject: Re: Pointers to freed memory
- Message-ID: <1992Jul31.141411.523@hubcap.clemson.edu>
- Organization: Clemson University, Clemson SC
- References: <3179@dozo.and.nl> <1992Jul30.164035.7349@taumet.com> <1992Jul31.035905.20683@oracle.us.oracle.com>
- Date: Fri, 31 Jul 1992 14:14:11 GMT
- Lines: 29
-
- In article <1992Jul31.035905.20683@oracle.us.oracle.com> wkaufman@us.oracle.com (William Kaufman) writes:
- >[...]
- > I can't count the times that I've freed a circular list the way the
- >original poster did--save the old "next" pointer, free this one, move on
- ^^^^^^^^^^^^^^ That's me.
- >to the saved "next". If what you say is true, you'd have to stop at
- >one-before-the-end and free that one without checking the next pointer.
-
- My solution, after the discussion here, was to break the circle before
- freeing:
- p = tail;
- q = p->next;
- p->next = NULL;
- while ( p ) {
- q = p->next;
- free(p);
- p = q;
- }
- Now, q will never contain the address of a freed element, and there is no
- special boundary case. (Note that insertions at the head of a singly-linked
- circular list are facilitated by pointing to the tail element instead of the
- head.)
-
-
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-