home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2605 < prev    next >
Encoding:
Text File  |  1992-09-10  |  2.8 KB  |  60 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!spool.mu.edu!darwin.sura.net!wupost!cs.utexas.edu!torn!watserv2.uwaterloo.ca!watmath!thinkage!dat
  3. From: dat@thinkage.on.ca (David Adrien Tanguay)
  4. Subject: Re: struct hack, and other out-of-array references
  5. Message-ID: <1992Sep10.213240.10272@thinkage.on.ca>
  6. Organization: Thinkage, Ltd.
  7. References: <1992Sep07.104932.20060@x.co.uk> <1992Sep8.124655.1498@Urmel.Informatik.RWTH-Aachen.DE> <1992Sep10.014137.16209@sq.sq.com>
  8. Date: Thu, 10 Sep 1992 21:32:40 GMT
  9. Lines: 49
  10.  
  11. msb@sq.sq.com (Mark Brader) writes:
  12. >The pointer f->s does point to an object with type char[1], but it is
  13. >also a pointer into the space returned by malloc(), which may be treated
  14. >as an array of chars.  That is, when strcpy computes the equivalent of
  15. >(f->s)[1], and therefore (f->s)+1, both the pointer operand of +, i.e.
  16. >f->s, and the computed result *are* within the same array, namely the one
  17. >returned by malloc(), and all is well.
  18.  
  19. How is this any different from the array example?
  20.  
  21.     struct { int i; char s[1]; } *f = malloc(sizeof(*f)+10);
  22.     int a[6][6];
  23.     int *p = a[1];
  24.     p += 7;   /* is this well-defined? */
  25.  
  26. If it is not defined, then it is the same as the struct hack. p may now point
  27. outside a[1], but it is still within the a object. Similarly, 
  28.     char *q = fs->s;
  29.     q += 2;
  30. is outside the f->s object but within the malloc object.
  31.  
  32. Also, I don't see how a[1][7] is any different from f->s[2]. The f->s object,
  33. determined by the "type and value", ends at f->s+1. Note that this case is
  34. different from the q case, above, since the type of q is different from the
  35. type of f->s.
  36.  
  37. (The interpretation, however, does deal only with arrays. We're extending the
  38. same reasoning to apply to structure elements, hoping for consistency.)
  39.  
  40. >I would also argue that the Interpretation Ruling cited above would not
  41. >apply if the type of the example array "a" had been char instead of int.
  42. >The definitions of "object" and "byte" (in ANSI section 1.6, ISO 3.14 and
  43. >3.4) in effect require that it be possible to treat any object as an
  44. >array of any character type.  This guarantee is assumed, of course, by
  45. >any number of library functions.  Once again, this should render legal any
  46. >references that are out of bounds in a subarray but which address locations
  47. >known to be in the containing array.
  48.  
  49. I don't see how this necessarily follows. "char *cp = a" would be allowed
  50. to range throughout a, satisfying 3.4, but "char *cp = a[1]" may be limited
  51. to ranging over the a[1] object.
  52.  
  53. I think this really is a matter of "interpretation": what is the "object"
  54. to which a pointer points? The object determined by the type of the
  55. originating pointer expression, or the largest C object containing the value
  56. originating pointer expression?
  57. -- 
  58. David Tanguay       dat@Thinkage.on.ca  dat@Thinkage.com  uunet!thinkage!dat
  59. Thinkage, Ltd.           Kitchener, Ontario, Canada          [43.40N 80.47W]
  60.