home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / std / c / 3340 < prev    next >
Encoding:
Text File  |  1993-01-08  |  1.6 KB  |  49 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: Re: Struct hack one last time (one last time)
  5. Message-ID: <1993Jan8.065947.29719@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <1993Jan7.221207.13818@leland.Stanford.EDU>
  8. Date: Fri, 8 Jan 93 06:59:47 GMT
  9. Lines: 38
  10.  
  11. > ... Again, the basic question is still unresolved here: can a conversion
  12. > from one pointer type to another affect bytes other than those
  13. > where the pointer variable is stored?
  14.  
  15. There is nothing in the standard to justify such an action, so it
  16. would be permissible only under the "'as if' rule".  It could, for
  17. example, modify bytes that the program mustn't read because the
  18. behavior would be undefined, such as the bytes of an object of
  19. indeterminate value.  It could modify the bytes of a volatile object.
  20. But it could NOT modify some arbitrary object with a well-defined
  21. value that the program correctly expects to persist!
  22.  
  23. In particular, it could not modify padding at the end of a struct,
  24. precisely *because* it would break the struct hack.  Another example
  25. of code that would break is:
  26.  
  27.     struct st {
  28.         /* anything */
  29.     } *stp;
  30.     union {
  31.         struct st st;
  32.         char chch[sizeof(struct st)];
  33.     } uu;
  34.     void *vp = &uu;
  35.     int i;
  36.  
  37.     for (i = 0; i < sizeof uu.chch; ++i)
  38.         uu.chch[i] = '#';
  39.     stp = vp;
  40.     for (i = 0; i < sizeof uu.chch; ++i)
  41.         assert (uu.chch[i] == '#');
  42.  
  43. -- 
  44. Mark Brader            \ "It's not in the slightest bit harder to write Fortran
  45. SoftQuad Inc., Toronto  \ or Basic programs in C++ or Smalltalk than it is
  46. utzoo!sq!msb, msb@sq.com \ to write them in C or Pascal."   -- Peter da Silva
  47.  
  48. This article is in the public domain.
  49.