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