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: Pointers to freed memory
- Message-ID: <1992Jul29.205834.11308@hubcap.clemson.edu>
- Organization: Clemson University, Clemson SC
- Date: Wed, 29 Jul 1992 20:58:34 GMT
- Lines: 25
-
- I know that a pointer to freed memory should never be dereferenced,
- but is it standard-conforming to assign or do arithmetic with such
- pointers?
-
- The application is a routine to free the elements of a circular list.
- Since I have a count of the elements, I can simply run a pair of pointers
- around the list, like so:
-
- for ( p = head, i = 0; i < n; i++ ) {
- q = p;
- p = p->next;
- free(q);
- }
-
- The question comes up because on the last iteration, p->next points
- to the element freed in the first iteration. Do I need to break the
- circle before I start the loop? Or am I just being paranoid after the
- discussion of pointers to invalid array elements?
-
- Thanks.
-
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-