home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / std / c / 2391 < prev    next >
Encoding:
Text File  |  1992-07-30  |  2.2 KB  |  64 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!taumet!steve
  3. From: steve@taumet.com (Steve Clamage)
  4. Subject: Re: Character arrays in structures and address alignment -- any standard?
  5. Message-ID: <1992Jul30.175841.7597@taumet.com>
  6. Organization: TauMetric Corporation
  7. References: <1992Jul27.183622.794@taumet.com> <1992Jul28.063356.28005@sq.sq.com> <14216@ksr.com> <Bs5pMJ.3nM@twwells.com>
  8. Date: Thu, 30 Jul 1992 17:58:41 GMT
  9. Lines: 53
  10.  
  11. bill@twwells.com (T. William Wells) writes:
  12.  
  13. >In article <14216@ksr.com> jfw@ksr.com (John F. Woods) writes:
  14. >: Please show a strictly conforming program which can tell the difference
  15. >: between "necessary" padding and "unnecessary" padding.
  16.  
  17. >For example:
  18.  
  19. >typedef struct {
  20. >    int     x;
  21. >    int     y;
  22. >} foo;
  23. >foo z;
  24.  
  25. >main()
  26. >{
  27. >    printf("%d\n", (offsetof(foo, y) - offset(foo, x)) - sizeof(int));
  28. >    return (0);
  29. >}
  30.  
  31. >should print 0 on any standards conforming compiler, if it is true
  32. >that the standard permits only padding for the purpose of
  33. >creating proper alignment.
  34.  
  35. A good case could be made for this particular example that 0 must be
  36. printed.  (The offset of x must be zero, so the example can be simplified).
  37.  
  38. The sizeof operator returns the size of an object as used in an array,
  39. with whatever padding is added.  One would then expect that array of
  40. two ints and "foo" would be the same size.  There is the possibility
  41. that padding in an array is not necessary but padding in a struct
  42. is necessary.  It seems unlikely, but I don't know that the Standard
  43. forbids such a situation.
  44.  
  45. The original example was something like
  46.     typedef struct {
  47.         char a[3];
  48.         char b[3];
  49.     }foo;
  50. and the question was whether offsetof(foo,b) must be 3.  I don't believe
  51. a strictly-conforming program could determine whether padding between
  52. fields "a" and "b" is "necessary", but only whether it was present.
  53.  
  54. "Necessary" is kind of a vague term.  On some architectures, alignment
  55. is absolutely required to avoid runtime faults.  Certainly this is
  56. "necessary".  On other architectures, alignment results in code which
  57. runs considerably faster at the expense of increased data space.  Is
  58. alignment then "necessary"?  I believe that is an economic, not a
  59. Standards, question.
  60. -- 
  61.  
  62. Steve Clamage, TauMetric Corp, steve@taumet.com
  63. Vice Chair, ANSI C++ Committee, X3J16
  64.