home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3424 < prev    next >
Encoding:
Text File  |  1993-01-26  |  4.0 KB  |  82 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!pipex!hunts.x.co.uk!clive
  3. From: clive@x.co.uk (Clive Feather)
  4. Subject: Re: Structure walking
  5. Message-ID: <C1GHJy.GJp@x.co.uk>
  6. Organization: IXI Limited
  7. References: <1993Jan25.213543.25499@pony.Ingres.COM> <1993Jan26.013634.20451@nntpd.lkg.dec.com>
  8. Date: Tue, 26 Jan 1993 10:09:33 GMT
  9. Lines: 71
  10.  
  11. In article <1993Jan26.013634.20451@nntpd.lkg.dec.com> diamond@jit.dec.com (Norman Diamond) writes:
  12. >In article <1993Jan25.213543.25499@pony.Ingres.COM> mikes@Ingres.COM (Mike Schilling) writes:
  13. >>(a) Each scalar type has an unambiguous alignment requirement.
  14. >For all existing C compilers?  Probably.  For all hypothetical standard
  15. >conforming compilers?  Uncertain.  Actually it is imaginable, for a
  16. >hypothetical but really meaningful compiler, if space is virtually free
  17. >but speed is extremely expensive, one might want to align a 4-byte integer
  18. >in the second half of an 8-byte cache line in order to save time by only
  19. >filling half of a cache line.  I don't know if you'd call this unambiguous.
  20.  
  21. ISO 3.1 defines alignment as the requirement for objects of a given type
  22. to have addresses that are multiples of some number. There are no
  23. requirements for alignment that I can find, except that 6.5.2.1 requires
  24. the alignment of structure members to be defined by the implementation.
  25.  
  26. >>(b) Each scalar type's alignment requirement is a power of two.
  27.  
  28. There is no such requirement in the standard.
  29.  
  30. >>(c) The offset of the first member of a structure is 0.
  31. >Probably.  A structure pointer cast to the type of the structure's first
  32. >member, or vice-versa, must still point to the right place.  And there cannot
  33. >be a "hole" at the beginning of a structure.  I'm not sure if this proves
  34. >that "offsetof" that member must yield a 0 result.
  35.  
  36. "offsetof" is defined as giving the offset of a member; this term is not
  37. further defined. Common-sense (which doesn't always work with the C
  38. standard) would say that your statement is true.
  39.  
  40. >>(d) The offset of the (n+1)th member of a structure is the offset of
  41. >>    the nth member of the structure, plus the size of the nth member,
  42. >>    rounded up to the alignment requirement of the type of the (n+1)th member.
  43. >I think the answer to (a) fits here.
  44.  
  45. It is arguable from the wording of 6.5.2.1 that only the minimal padding
  46. may be provided (in other words, item (d) is true), but Norman's answer
  47. to (a) makes it effectively useless (i.e. there is no way to determine
  48. the alignment requirement of the type of the (n+1)th member without
  49. reading the vendor's manual (which must state it)).
  50.  
  51. >>(e) The alignment required by the structure is equal to the most restrictive
  52. >>    alignment of any of its members.
  53. >No.  It seems to be mostly agreed (though I'm not sure if I agree) that the
  54. >standard permits, and in some cases compilers do, align structures more
  55. >strictly than the most restrictive alignment of any of its members.
  56.  
  57. Again, 3.1 allows this provided that the vendor documents it.
  58.  
  59. >>(f) The size of the structure is the offset of the last member of the structure,
  60. >>    plus the size of the last member, rounded up to alignment required
  61. >>    by the structure.
  62. >I think the answer to (a) fits again here.
  63.  
  64. Ditto. The answer to (e) makes this impossible to determine programatically.
  65.  
  66. >>Question 3:  On at least one platform, pointers to characters were represented
  67. >>diferently from pointers to anything else
  68. >>On such platforms, how are pointers to structures represented?
  69. >I think it's unspecified.  The implementation has to do something that will
  70. >work, but I don't think they have to tell you what they've done.
  71.  
  72. Indeed. Note that on such an implementation, a structure containing one
  73. "char" field may need a alignment of (say) 8, precisely so that
  74. "pointers to anything else" can point to them. This justifies the answer
  75. to (e).
  76.  
  77. -- 
  78. Clive D.W. Feather     | IXI Limited         | If you lie to the compiler,
  79. clive@x.co.uk          | Vision Park         | it will get its revenge.
  80. Phone: +44 223 236 555 | Cambridge   CB4 4RZ |   - Henry Spencer
  81. Fax:   +44 223 236 550 | United Kingdom      |
  82.