B.29 Structure Alignment

The alignment and size of a structure is affected by the alignment requirements and sizes of the structure components for each platform. A structure can begin on any byte boundary and occupy any integral number of bytes. However, individual architectures or operating systems can specify particular default alignment and padding requirements, which can be overridden by pragmas and command-line options.

OpenVMS Alpha and Digital UNIX

On OpenVMS Alpha and Digital UNIX systems, nonbit-field structure members are, by default, aligned on natural boundaries.

The default alignment of a structure is the maximum alignment required by any member within the structure. The structure is padded to ensure that the size of a structure, in bytes, is a multiple of its alignment requirement to achieve the appropriate alignment when the structure or union is a member of an array.

The components of a structure are laid out in memory in the order they are declared. The first component has the same address as the entire structure. Padding is introduced between components to satisfy the alignment requirements of individual components.

A bit field can have any integral type. However, the compiler issues a warning with the error-checking option if the type is anything other than int , unsigned int , or signed int . The presence of bit fields causes the alignment of the whole structure or union to be at least the same as that of the bit field's base type.

Bit fields (including zero-length bit fields) not immediately declared following other bit fields have the alignment requirement imposed by their base type. Bit fields are allocated within the alignment unit (of the same size as the bit field's base type) from low-order to high-order.

With #pragma member_alignment in effect, if a bit field immediately follows another bit field, the bits are packed into adjacent space in the same unit, if sufficient space remains. Otherwise, padding is inserted at the end of the first bit field and the second bit field is put into the next unit.

With #pragma nomember_alignment in effect, bit fields are allowed to span storage unit boundaries. Alpha systems default to member_alignment while VAX systems default to nomember_alignment .

Bit fields of base type char cannot be larger than 8 bits. Bit fields of base type short cannot be larger than 16 bits.

OpenVMS VAX

OpenVMS VAX systems do not require that structures or structure members be aligned on any particular boundaries; nonbit-field structure members are byte-aligned by default.

The components of a structure are laid out in memory in the order they are declared. The first component has the same address as the entire structure. Each additional component follows its predecessor in the immediately following byte.

Natural alignment of structure members, can be obtained by using the following pragma:

pragma member_alignment

The DEC C User's Guide for OpenVMS Systems has examples and diagrams of OpenVMS VAX structure alignment.

Bit fields can have any integral type. However, the compiler issues a warning if /STANDARD=ANSI89 is specified, and the type is other than int , unsigned int , or signed int . Bit fields are allocated within the unit from low order to high order. If a bit field immediately follows another bit field, the bits are packed into adjacent space, even if this overflows into another byte. However, if an unnamed bit field is specified to have length 0, filler is added so the bit field immediately following starts on the next byte boundary.

The DEC C User's Guide for OpenVMS Systems has examples and diagrams of OpenVMS VAX bit-field alignment.


Previous Page | Next Page | Table of Contents | Index