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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gatech!hubcap!mjs
  3. From: mjs@hubcap.clemson.edu (M. J. Saltzman)
  4. Subject: Pointers to freed memory
  5. Message-ID: <1992Jul29.205834.11308@hubcap.clemson.edu>
  6. Organization: Clemson University, Clemson SC
  7. Date: Wed, 29 Jul 1992 20:58:34 GMT
  8. Lines: 25
  9.  
  10. I know that a pointer to freed memory should never be dereferenced, 
  11. but is it standard-conforming to assign or do arithmetic with such
  12. pointers?
  13.  
  14. The application is a routine to free the elements of a circular list.
  15. Since I have a count of the elements, I can simply run a pair of pointers
  16. around the list, like so:
  17.  
  18.     for ( p = head, i = 0;  i < n;  i++ ) {
  19.         q = p;
  20.         p = p->next;
  21.         free(q);
  22.     }
  23.  
  24. The question comes up because on the last iteration, p->next points
  25. to the element freed in the first iteration.  Do I need to break the
  26. circle before I start the loop? Or am I just being paranoid after the 
  27. discussion of pointers to invalid array elements?
  28.  
  29. Thanks.
  30.  
  31. -- 
  32.         Matthew Saltzman
  33.         Clemson University Math Sciences
  34.         mjs@clemson.edu
  35.