home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11771 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  2.3 KB

  1. Path: sparky!uunet!crdgw1!rdsunx.crd.ge.com!bart!volpe
  2. From: volpe@bart.NoSubdomain.NoDomain (Christopher R Volpe)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Pointers to freed memory
  5. Message-ID: <1992Jul30.180908.1013@crd.ge.com>
  6. Date: 30 Jul 92 18:09:08 GMT
  7. References: <1992Jul29.205834.11308@hubcap.clemson.edu> <3179@dozo.and.nl>
  8. Sender: volpe@bart (Christopher R Volpe)
  9. Reply-To: volpe@ausable.crd.ge.com
  10. Organization: GE Corporate Research & Development
  11. Lines: 49
  12. Nntp-Posting-Host: bart.crd.ge.com
  13.  
  14. In article <3179@dozo.and.nl>, jos@and.nl (Jos Horsmeier) writes:
  15. |> In article <1992Jul29.205834.11308@hubcap.clemson.edu> mjs@hubcap.clemson.edu (M. J. Saltzman) writes:
  16. |> |I know that a pointer to freed memory should never be dereferenced, 
  17. |> |but is it standard-conforming to assign or do arithmetic with such
  18. |> |pointers?
  19. |> |
  20. |> |The application is a routine to free the elements of a circular list.
  21. |> |Since I have a count of the elements, I can simply run a pair of pointers
  22. |> |around the list, like so:
  23. |> |
  24. |> |    for ( p = head, i = 0;  i < n;  i++ ) {
  25. |> |        q = p;
  26. |> |        p = p->next;
  27. |> |        free(q);
  28. |> |    }
  29. |> |
  30. |> |The question comes up because on the last iteration, p->next points
  31. |> |to the element freed in the first iteration.  Do I need to break the
  32. |> |circle before I start the loop? Or am I just being paranoid after the 
  33. |> |discussion of pointers to invalid array elements?
  34. |> 
  35. |> Erm, yes, I hate to say it, but you're getting paranoid ;-) 
  36.  
  37. I'd say that's true for most real compilers, but...
  38.  
  39. |> 
  40. |> As long as you do not dereference such a pointer (as you indicated
  41. |> yourself,) everything is fine.  If pointer q contained a valid pointer
  42.  
  43. I disagree that this will always be true. There is nothing to prevent
  44. an implementation from trapping assignments of invalid addresses to
  45. pointer variables. I wouldn't be surprised if a debugging C interpreter
  46. (ala Saber) did this someday. 
  47.  
  48. |> value _before_ the call to free(), it'll contain a valid pointer value
  49. |> _after_ the call. In the last iteration, the value p->next points to
  50. |> a location in memory where the first element of the list _would have
  51. |> been_ if it weren't freed in the first iteration. And, again, if you
  52. |> don't dereference that pointer value, everything is safe and sound.
  53. |> 
  54. |> kind regards,
  55. |> 
  56. |> Jos aka jos@and.nl
  57.  
  58. -- 
  59. ==================
  60. Chris Volpe
  61. G.E. Corporate R&D
  62. volpecr@crd.ge.com
  63.