home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11814 < prev    next >
Encoding:
Text File  |  1992-07-31  |  1.3 KB  |  40 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!hubcap!mjs
  3. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  4. Subject: Re: Pointers to freed memory
  5. Message-ID: <1992Jul31.141411.523@hubcap.clemson.edu>
  6. Organization: Clemson University, Clemson SC
  7. References: <3179@dozo.and.nl> <1992Jul30.164035.7349@taumet.com> <1992Jul31.035905.20683@oracle.us.oracle.com>
  8. Date: Fri, 31 Jul 1992 14:14:11 GMT
  9. Lines: 29
  10.  
  11. In article <1992Jul31.035905.20683@oracle.us.oracle.com> wkaufman@us.oracle.com (William Kaufman) writes:
  12. >[...]
  13. >    I can't count the times that I've freed a circular list the way the
  14. >original poster did--save the old "next" pointer, free this one, move on
  15.  ^^^^^^^^^^^^^^ That's me.
  16. >to the saved "next".  If what you say is true, you'd have to stop at
  17. >one-before-the-end and free that one without checking the next pointer.
  18.  
  19. My solution, after the discussion here, was to break the circle before
  20. freeing:
  21.     p = tail;
  22.     q = p->next;
  23.     p->next = NULL;
  24.     while ( p ) {
  25.       q = p->next;
  26.       free(p);
  27.       p = q;
  28.     }
  29. Now, q will never contain the address of a freed element, and there is no
  30. special boundary case.  (Note that insertions at the head of a singly-linked
  31. circular list are facilitated by pointing to the tail element instead of the 
  32. head.)
  33.  
  34.  
  35.  
  36. -- 
  37.         Matthew Saltzman
  38.         Clemson University Math Sciences
  39.         mjs@clemson.edu
  40.