home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / std / c / 3003 < prev    next >
Encoding:
Text File  |  1992-11-13  |  2.4 KB  |  53 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!sdd.hp.com!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: fwrite+fread of pointer
  5. Message-ID: <1992Nov13.101813.163@nntpd.lkg.dec.com>
  6. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  7. Reply-To: diamond@jit.dec.com (Norman Diamond)
  8. Organization: Digital Equipment Corporation Japan , Tokyo
  9. References: <15935@goanna.cs.rmit.oz.au>
  10. Date: Fri, 13 Nov 1992 10:18:13 GMT
  11. Lines: 40
  12.  
  13. In article <15935@goanna.cs.rmit.oz.au> ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
  14. >    #include <stdlib.h>
  15. >    #include <stdio.h>
  16. >    int main(argc, argv) int argc; char **argv; {
  17. >        char dummy;
  18. >        char *p = &dummy;
  19. >        FILE *f = fopen("WORK", "w+b");
  20. >        fwrite(&p, sizeof p, 1, f);
  21. >        rewind(f);
  22. >        fread(&p, sizeof p, 1, f);
  23. >        fclose(f);
  24. >        printf("%d\n", p == &dummy);
  25. >        exit(0);
  26. >        }
  27. >What I want to know is whether, assuming nothing else goes wrong, the
  28. >program _must_ print 1 or whether it may print 0.
  29.  
  30. ANSI 4.9.2, page 126 lines 33 to 34:  "Data read in from a binary stream
  31. shall compare equal to the data that were earlier written out to that
  32. stream, under the same implementation."  So I almost say it must print 1.
  33.  
  34. The clause "under the same implementation" can only mean that the file
  35. might be read by a different program (or different invocation of the same
  36. program) than the one that wrote the file, and as long as those were
  37. translated by the same implementation, the data must remain equal.
  38. But of course, in a different program, the variable "p" is likely to be
  39. at a different location.  We should reasonably expect this to be an
  40. exception, but the standard doesn't hint at an exception.  This troubles me.
  41.  
  42. The real reason I am troubled is that until now, I thought that it was
  43. theoretically possible to implement garbage collection in C.  Each pointer
  44. would have to be a handle and each object would need additional information
  45. so that when an object is moved, all of the pointers in the program could
  46. be updated automatically.  But this would yield a second exception to this
  47. part of the standard -- and it could yield a 0 for Dr. O'Keefe's program.
  48. Perhaps an interpretation ruling should say how many exceptions are allowable.
  49. --
  50. Norman Diamond       diamond@jit081.enet.dec.com
  51. If this were the company's opinion, I wouldn't be allowed to post it.
  52. "It's been a lovely recession."
  53.