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