home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2606 < prev    next >
Encoding:
Internet Message Format  |  1992-09-10  |  1.6 KB

  1. Path: sparky!uunet!elroy.jpl.nasa.gov!usc!rpi!utcsri!dgp.toronto.edu!flaps
  2. Newsgroups: comp.std.c
  3. From: flaps@dgp.toronto.edu (Alan J Rosenthal)
  4. Subject: Re: struct hack, and other out-of-array references
  5. Message-ID: <1992Sep11.012438.24377@jarvis.csri.toronto.edu>
  6. References: <1992Sep07.104932.20060@x.co.uk> <1992Sep8.124655.1498@Urmel.Informatik.RWTH-Aachen.DE> <1992Sep10.014137.16209@sq.sq.com> <1992Sep10.213240.10272@thinkage.on.ca>
  7. Date: 11 Sep 92 05:24:38 GMT
  8. Lines: 37
  9.  
  10. dat@thinkage.on.ca (David Adrien Tanguay) writes:
  11. >How is this any different from the array example?  p may point
  12. >outside a[1], but it is still within the a object.  Similarly, ...
  13. >is outside the f->s object but within the malloc object.
  14.  
  15. The point is, "int a[5][5];" doesn't give you 25 ints, it gives you five arrays
  16. of five ints each.  "(int(*)[5])malloc(5*sizeof(int[5]))" gives you
  17. sizeof(int)*25 bytes which are ALSO five arrays of five ints.
  18.  
  19. The first looks like this:
  20.     int[5][5]:
  21.         int[5]:
  22.         int
  23.         int
  24.         int
  25.         int
  26.         int
  27.         int[5]:
  28.         ...
  29.         ...
  30.  
  31. And the second looks like this:
  32.     char
  33.     char
  34.     char
  35.     char
  36.     ...
  37.  
  38. Now that cast magically makes the second be close enough to the first in
  39. outward appearance for all intents and purposes, but it's still the second
  40. underneath.  Obviously in almost all compilers the memory layout will be the
  41. same.  However, an environment such as Saber-C should be free to keep track of
  42. the details and that's why I've always thought that this is a good
  43. interpretation ruling.
  44.  
  45. I'm not sure what you get when you take a pointer to that array five of array
  46. five of int, cast it to a (char *), then cast it to a (int(*)[5]).
  47.