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

  1. Path: sparky!uunet!ogicse!decwrl!world!ksr!jfw
  2. From: jfw@ksr.com (John F. Woods)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Memory alignment of struct elements
  5. Message-ID: <14806@ksr.com>
  6. Date: 14 Aug 92 17:15:30 GMT
  7. Article-I.D.: ksr.14806
  8. References: <1992Aug12.133132.10723@Princeton.EDU>
  9. Sender: news@ksr.com
  10. Lines: 24
  11.  
  12. udalski@astro.Princeton.EDU (Andrzej Udalski) writes:
  13. >I am writing the software for a big database and the space is very
  14. >important. If I try to use a 
  15. >struct { char ch ; float f }
  16. >the compiler (both in SunOS 4.0.1 and Interactive SYSVR3) allocates
  17. >8 bytes for it, starting the "f" element at fifth byte of the structure.
  18. >This makes both reading and writing such records from/to a binary database 
  19. >file (consisting of 5-byte-long records) very inconvinient, requiring
  20. >a declaration of character array of 5 bytes and using "memcpy()". Is there 
  21. >a way to force the compiler to align the struct elements following their 
  22. >true length?
  23.  
  24. It is possible that the architecture of the machine you are running on forbids
  25. addressing multiple-byte quantities on non-aligned boundaries (I'm pretty
  26. sure the SPARC forbids it).  You're almost certain to take a serious
  27. performance penalty if it is permitted at all.  If the database has not
  28. already been constructed, pad things to four-byte boundaries to make your life
  29. easier, or (better yet) design your software to assume it is necessary to
  30. unpack the on-disk structures into internal variables, as you're already doing.
  31. Making that assumption will make it easier to change the internal layout (to
  32. go from one machine to another, for example, or to change the internal float
  33. to double precision).  In the modern world, where computer centers change
  34. processor architectures frequently, writing in-core data directly out to disk
  35. is asking for trouble.
  36.