home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / std / c / 3426 < prev    next >
Encoding:
Internet Message Format  |  1993-01-27  |  3.8 KB

  1. Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!mailgzrz.TU-Berlin.DE!math.fu-berlin.de!ira.uka.de!sol.ctr.columbia.edu!emory!gatech!hubcap!ncrcae!ncrhub2!ncrgw2!psinntp!heimdall!thor!scjones
  2. From: scjones@thor.sdrc.com (Larry Jones)
  3. Newsgroups: comp.std.c
  4. Subject: Re: Structure walking
  5. Message-ID: <565@heimdall.sdrc.com>
  6. Date: 26 Jan 93 15:44:26 GMT
  7. References: <1993Jan25.213543.25499@pony.Ingres.COM>
  8. Sender: news@heimdall.sdrc.com
  9. Lines: 75
  10.  
  11. In article <1993Jan25.213543.25499@pony.Ingres.COM>, mikes@Ingres.COM (Mike Schilling) writes:
  12. > Question 1:  Are the following assumptions valid for all C compilers?
  13. > For (c)-(f), assume that the structure contains only scalar members.
  14. > (a) Each scalar type has an unambiguous alignment requirement.
  15.  
  16. True, but the compiler may choose to use a stricter alignment than is
  17. actually required.
  18.  
  19. > (b) Each scalar type's alignment requirement is a power of two.
  20.  
  21. False.  The Standard contains no such guarantee.
  22.  
  23. > (c) The offset of the first member of a structure is 0.
  24.  
  25. True.  ANSI 3.5.2.1 Structure and Union Specifiers:
  26.     A pointer to a structure object, suitably converted, points to
  27.     its initial member (or if that member is a bit-field, then to
  28.     the unit in which it resides), and vice versa.  There may
  29.     therefore be unnamed padding within a structure object, but not
  30.     at its beginning, as necessary to achieve the appropriate
  31.     alignment.
  32.  
  33. > (d) The offset of the (n+1)th member of a structure is the offset of
  34. >     the nth member of the structure, plus the size of the nth member,
  35. >     rounded up to the alignment requirement of the type of the (n+1)th member.
  36.  
  37. False.  ANSI 3.5.2.1 (emphasis mine):
  38.     Each non-bit-field member of a structure or union object is
  39.     aligned in an implementation-defined manner *appropriate* to its
  40.     type.
  41. Note that it says "appropriate to", not "required by".  An
  42. implementation has a great deal of latitude in how it aligns members.
  43.  
  44. > (e) The alignment required by the structure is equal to the most restrictive
  45. >     alignment of any of its members.
  46.  
  47. True, but see (a).
  48.  
  49. > (f) The size of the structure is the offset of the last member of the structure,
  50. >     plus the size of the last member, rounded up to alignment required
  51. >     by the structure.
  52.  
  53. False.  ANSI 3.5.2.1 again:
  54.     There may also be unnamed padding at the end of a structure or
  55.     union, as necessary to achieve the *appropriate* alignment were
  56.     the structure or union to be an element of an array.
  57. Again, it says "appropriate", not "required".
  58.  
  59. > Question 2:  Can the assumptions in question 1 be generalized to include
  60. > structures which contain nested structures?  Equivalently: if we "flatten"
  61. > a structure (eliminate all nesting), will that leave its size, alignment
  62. > requirements, and the offsets of its scalar members unchanged?
  63.  
  64. The answers don't change if you allow nested structures but, since your
  65. assumptions don't hold, you can't flatten a structure and expect its
  66. size, alignment, and member offsets to remain the same.
  67.  
  68. > Question 3:  On at least one platform (Data General MV series minis),
  69. > pointers to characters were represented diferently from pointers to anything
  70. > else (really a byte pointer vs. word pointer distinction), so you had 
  71. > to be very careful about how you cast pointers. 
  72. > On such platforms, how are pointers to structures represented?  Does 
  73. > it depend on the type of the structure?
  74.  
  75. It is generally reguarded as true that the Standard contains sufficient
  76. requirements scattered about its various sections to require that all
  77. structure pointers have the same representation.  There is no guarantee
  78. that this representation is identical to the representation of any other
  79. pointer type, however.
  80. ----
  81. Larry Jones, SDRC, 2000 Eastman Dr., Milford, OH  45150-2789  513-576-2070
  82. larry.jones@sdrc.com
  83. If I get a bad grade, it'll be YOUR fault for not doing
  84. the work for me! -- Calvin
  85.