This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: sizeof and allignment on 32bit target
On Fri, Jul 25, 2003 at 11:28:05AM +0200, Eric de Jong wrote:
> Thank you, Jan Zizka, Roman Kellner
>
> I would expect that the allignment has only influence on the address of
> variables, not on the total size of a structure. It looks like the compiler
> calculates the size of the structure to be a multiple of the largest item in the
> structure.
> What I want is to allign each variable in the structure to it's own size, but
> the total size of the structure to be the actual size. Is this possible?
Without using packed and aligned attributes, this is not possible. This can be
a severe performance penalty on many architectures and are not supported by
other compilers.
According to the ISO C90 and C99 standards, the alignment of a structure or
union is at least the maximum of the alignments of each of its members. A
compiler can make structure and unions have even more alignment than the
alignment of the members, for example some ABI's on word based machines will
align any structure to a word boundary, even if all of the fields are chars,
and on the machine I just did an ABI for, the compiler will align any structure
with 2 or more words to double word alignment so the double word memory
instructions can be more often used. This wording has been in the C
specification ever since I can remember (and I used C on UNIX V6 PDP-11
machines).
The reason for this is to make arrays and p++ work correctly. If the size were
smaller, p++ would wind up pointing to unalgined memory.
Note, also you are not guaranteed that short/int/long are the same size or
alignment on different machines either. If you are reading files produced by
another computer, you have to worry about endian issues as well.
--
Michael Meissner
email: gnu@the-meissners.org
http://www.the-meissners.org