home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12274 < prev    next >
Encoding:
Internet Message Format  |  1992-08-13  |  2.6 KB

  1. Path: sparky!uunet!usc!sdd.hp.com!swrinde!gatech!concert!sas!mozart.unx.sas.com!sasghm
  2. From: sasghm@theseus.unx.sas.com (Gary Merrill)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory alignment of struct elements
  5. Message-ID: <BsxCtz.598@unx.sas.com>
  6. Date: 13 Aug 92 13:44:23 GMT
  7. References: <1992Aug12.133132.10723@Princeton.EDU>
  8. Sender: news@unx.sas.com (Noter of Newsworthy Events)
  9. Organization: SAS Institute Inc.
  10. Lines: 44
  11. Originator: sasghm@theseus.unx.sas.com
  12. Nntp-Posting-Host: theseus.unx.sas.com
  13.  
  14.  
  15. In article <1992Aug12.133132.10723@Princeton.EDU>, udalski@astro.Princeton.EDU (Andrzej Udalski) writes:
  16. |> I am writing the software for a big database and the space is very
  17. |> important. If I try to use a 
  18. |> 
  19. |> struct { char ch ; float f }
  20. |> 
  21. |> the compiler (both in SunOS 4.0.1 and Interactive SYSVR3) allocates
  22. |> 8 bytes for it, starting the "f" element at fifth byte of the structure.
  23. |> This makes both reading and writing such records from/to a binary database 
  24. |> file (consisting of 5-byte-long records) very inconvinient, requiring
  25. |> a declaration of character array of 5 bytes and using "memcpy()". Is there 
  26. |> a way to force the compiler to align the struct elements following their 
  27. |> true length?
  28.  
  29. This depends upon the compiler, of course.  Many compilers have such
  30. an option, but (depending upon the architecture, etc.) it may not
  31. be a good idea to use it.  You might simply try:
  32.  
  33.     struct { float f; char ch; }
  34.  
  35. which will not require the internal padding you are encountering.
  36. In general, to avoid such padding you should define your structure
  37. members in decreasing order of alignment requirements.  Note that
  38. the compiler will tend to add padding to the *end* of the structure
  39. in order to achieve correct alignment in *arrays* of such structures.
  40. Still, such an approach may help you.
  41.  
  42. I would be a little concerned about the maintainability of your code,
  43. however. If things are done in a careful and modular manner (and,
  44. for example, sizes of records, etc. are not "hardcoded"), I do not
  45. see much inconvenience in going to/from the database via an intermediate
  46. buffer.  The danger you need to be aware of, of course, is the
  47. possibility that in the future someone decides to add a new field to
  48. your structure, and either adds it in the "wrong" place (for padding
  49. purposes) or forgets to update routines that access it.  Whatever you
  50. do, you should choose safety and maintainability over any small
  51. gain in what might be thought of as "convenience".
  52.  
  53.  
  54. -- 
  55. Gary H. Merrill  [Principal Systems Developer, C Compiler Development]
  56. SAS Institute Inc. / SAS Campus Dr. / Cary, NC  27513 / (919) 677-8000
  57. sasghm@theseus.unx.sas.com ... !mcnc!sas!sasghm
  58.