home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16987 < prev    next >
Encoding:
Text File  |  1992-11-22  |  2.7 KB  |  55 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!uunet.ca!wildcan!sq!msb
  3. From: msb@sq.sq.com (Mark Brader)
  4. Subject: struct hack (was: Is this ANSI?)
  5. Message-ID: <1992Nov22.134020.16968@sq.sq.com>
  6. Organization: SoftQuad Inc., Toronto, Canada
  7. References: <24238@alice.att.com> <By0vu2.7AF@unx.sas.com> <24262@alice.att.com>
  8. Date: Sun, 22 Nov 92 13:40:20 GMT
  9. Lines: 44
  10.  
  11. > >     struct FM { short data_len; char data[1]; } *fmptr;
  12. > >     fmptr = malloc( sizeof(struct FM) + ??? );
  13. > > 
  14. > > where the ??? is the number of bytes of your data minus 1.  Some may
  15. > > regard this as "impure", but it is not forbidden by the standard and
  16. > > is eminently portable.
  17. > Oh, really?  Can you give me concrete evidence that the C standard allows
  18. > it?  More specifically, after executing the code above, can you give me
  19. > evidence that referrint to fmptr->data[i] is allowed when i>1?
  20.  
  21. I'll be very brief here, because we *just* went through this in
  22. *excruciating detail* in comp.std.c, and my typing fingers are
  23. still a bit raw. :-)  I think most people would be either (a)
  24. reading that newsgroup and have been following the discussion
  25. there, or (b) willing to accept the comp.lang.c FAQ list's correct
  26. statement that "the ANSI standard allows it only implicitly", or
  27. (c) willing to take disputes about the FAQ list to *email*.
  28.  
  29. [1] 4.10.3 and 4.10.3.3 require malloc(), if successful, to return an
  30.     object which may be addressed as an array of any type of object.
  31. [2] 3.3.2.1 makes fmptr->data[i] equivalent to *(fmptr->data + i).
  32. [3] 3.1.2.5 says that a pointer contains a reference to an object and
  33.     says nothing to allow it to possibly containing other information.
  34.     Therefore fmptr->data is equivalent to p+offsetof(struct FM,data)
  35.     where p is the pointer returned by malloc() converted to char *.
  36. [4] 3.3.6 allows an offset to be added to a pointer provided that
  37.     both the original pointer value [here fmptr->data or p+offsetof
  38.     (struct FM,data)] and the result [these expressions plus ???]
  39.     are within the same array object.  In this case, if the result
  40.     is within the array object returned by malloc(), they are within
  41.     the same array object, so the bounds on "data" are irrelevant.
  42.  
  43. This is different from the case of "int x[5][5]; int i = x[1][7];"
  44. which has been correctly determined in an Interpretation Ruling to
  45. not be allowed under 3.3.6; there is nothing in the standard to allow
  46. this indexing, even though the 25 ints making up "x" are required to
  47. all be adjacent.
  48. -- 
  49. Mark Brader          "Should array indices start at 0 or 1?  My ecumenical
  50. SoftQuad Inc., Toronto       compromise of 0.5 was rejected without, I thought,
  51. utzoo!sq!msb, msb@sq.com   proper consideration."  -- Stan Kelly-Bootle
  52.  
  53. This article is in the public domain.
  54.