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

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!twwells!bill
  3. From: bill@twwells.com (T. William Wells)
  4. Subject: Re: Character arrays in structures and address alignment -- any standard?
  5. Organization: None, Mt. Laurel, NJ
  6. References: <1992Jul27.183622.794@taumet.com> <1992Jul28.063356.28005@sq.sq.com> <14216@ksr.com>
  7. Message-ID: <Bs5pMJ.3nM@twwells.com>
  8. Date: Wed, 29 Jul 1992 15:27:53 GMT
  9. Lines: 35
  10.  
  11. In article <14216@ksr.com> jfw@ksr.com (John F. Woods) writes:
  12. : msb@sq.sq.com (Mark Brader) writes:
  13. : >> The Standard allows arbitrary padding between fields and after the
  14. : >> last field.
  15. : >Not quite; it allows padding so that the members are correctly aligned
  16. : >for their types.  It does not say anything about padding being permitted
  17. : >for any other reasons, and my interpretation is that it isn't.
  18. :
  19. : Please show a strictly conforming program which can tell the difference
  20. : between "necessary" padding and "unnecessary" padding.
  21.  
  22. For example:
  23.  
  24. typedef struct {
  25.     int     x;
  26.     int     y;
  27. } foo;
  28. foo z;
  29.  
  30. main()
  31. {
  32.     printf("%d\n", (offsetof(foo, y) - offset(foo, x)) - sizeof(int));
  33.     return (0);
  34. }
  35.  
  36. should print 0 on any standards conforming compiler, if it is true
  37. that the standard permits only padding for the purpose of
  38. creating proper alignment. (I'll not argue the question of
  39. whether it does. Since it would be a daft compiler that added
  40. padding for any reason other than alignment, I think it not worth
  41. arguing.)
  42.  
  43. ---
  44. Bill                            { uunet | decwrl | telesci }!twwells!bill
  45. bill@twwells.com
  46.