Rules for field alignment
Michael Meissner
meissner@cygnus.com
Tue Jun 27 10:09:00 GMT 2000
On Tue, Jun 27, 2000 at 04:27:28PM +0530, Radhika S wrote:
> Hello,
>
> Can someone please send the rules followed in the gcc compiler 2.95.2
> for field alignments inside structures?
GCC uses different rules for alignment of fields and structures, based on the
ABI of the target. In general if there is a wacky way to pad and align
structures that meets the standard (address of the first element of a structure
when suitably converted is the same as the structure address, addresses in a
struct are monotonically increasing and all addresses of union members when
suitable converted are the same, alignment of the entire structure is at least
as much as the largest alignment of members), some GCC port probably implements
it. Some things that I can think of off the top of my head:
1) Whether the type of a bitfield influences the alignment of the whole
structure (ie, struct { int x:1; } takes 4 or 8 bytes, while
struct { char x:1; } takes 1 byte).
2) RISC platforms generally want native alignment (chars aligned on 1 byte
boundaries, shorts on 2 byte boundaries, ints on 4 or 8 byte
boundaries, and the entire structure alginment is the max of the member
alignments).
3) Both the 68k and 386 ABIs cater to the quirks of the original
implementation to the detrement of future generations of the chip
(IIRC, the 68k uses 2 byte alignment instead of native alignment, the
386 does not align doubles to 8 byte boundaries).
4) AIX aligns doubles to 32-bits within structures, but if the first field
in a structure is a double, it will align the entire structure to 64
bits.
--
Michael Meissner, Red Hat, Inc.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work: meissner@redhat.com phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org fax: +1 978-692-4482
More information about the Gcc-bugs
mailing list