home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.std.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: Struct hack one last time (one last time)
- Message-ID: <1993Jan8.065947.29719@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <1993Jan7.221207.13818@leland.Stanford.EDU>
- Date: Fri, 8 Jan 93 06:59:47 GMT
- Lines: 38
-
- > ... Again, the basic question is still unresolved here: can a conversion
- > from one pointer type to another affect bytes other than those
- > where the pointer variable is stored?
-
- There is nothing in the standard to justify such an action, so it
- would be permissible only under the "'as if' rule". It could, for
- example, modify bytes that the program mustn't read because the
- behavior would be undefined, such as the bytes of an object of
- indeterminate value. It could modify the bytes of a volatile object.
- But it could NOT modify some arbitrary object with a well-defined
- value that the program correctly expects to persist!
-
- In particular, it could not modify padding at the end of a struct,
- precisely *because* it would break the struct hack. Another example
- of code that would break is:
-
- struct st {
- /* anything */
- } *stp;
- union {
- struct st st;
- char chch[sizeof(struct st)];
- } uu;
- void *vp = &uu;
- int i;
-
- for (i = 0; i < sizeof uu.chch; ++i)
- uu.chch[i] = '#';
- stp = vp;
- for (i = 0; i < sizeof uu.chch; ++i)
- assert (uu.chch[i] == '#');
-
- --
- Mark Brader \ "It's not in the slightest bit harder to write Fortran
- SoftQuad Inc., Toronto \ or Basic programs in C++ or Smalltalk than it is
- utzoo!sq!msb, msb@sq.com \ to write them in C or Pascal." -- Peter da Silva
-
- This article is in the public domain.
-