Data Type Alignments [ HP C Programmer's Guide ] MPE/iX 5.0 Documentation
HP C Programmer's Guide
Data Type Alignments
This section illustrates the various alignment modes of the HP 9000, HP
3000, and HP Apollo systems as well as the storage and alignment rules of
VAX/VMS C, CCS/1000, and CCS/C 3000.
The data alignment modes provided on the various HP and HP Apollo systems
can be divided into two main categories, WORD and NATURAL. WORD alignment
modes are typical of MPE V systems. The most restricted alignment
boundary of WORD alignment modes is 2 bytes. NATURAL alignment modes are
typical of RISC-based systems where the alignment of an object is related
to the size of the object.
The WORD alignments are:
HPUX_WORD The native alignment for HP 9000 Series 300/400.
DOMAIN_WORD The native alignment for HP Apollo Series 3000/4000.
MPE_16 The native alignment for HP 3000 Series 4x/5x/6x/7x
(MPE V).
The NATURAL alignments are:
HPUX_NATURAL The native alignment for HP 9000 Series 700/800 and HP
3000 Series 900.
DOMAIN_NATURAL The native alignment for HP Apollo Series 10000.
NATURAL An architecture-independent alignment mode for HP 9000
Series 300/400/700/800, and HP Apollo Series 3000,
4000, 10000.
The alignment modes listed above can be controlled using the HP_ALIGN
compiler pragma. See the section titled "Declaring the HP_ALIGN Pragma"
in this chapter for a detailed description of this pragma. The NATURAL
alignment mode should be used whenever possible. This mode enables data
to be shared among the greatest number of HP-UX and Domain (HP Apollo)
systems.
The following table presents the differences between the various
alignment modes. The alignment modes of other C compilers are also
shown.
Table 2-1. Comparison of Data Types and Alignments
-------------------------------------------------------------------------------------------------------
| | | | | | |
| Data Type | Size | Alignment | Alignment | Alignment | Alignment |
| | (bytes) | HPUX_WORD | HPUX_NATURAL | HPUX_NATURAL_ | NATURAL |
| | | DOMAIN_WORD | (Series | S500 | |
| | | MPE_16 | 700/800) | | |
| | | | DOMAIN_NATURAL | | |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| char enum | 1 | 1-byte | 1-byte | 1-byte | 1-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| char, signed | 1 | 1-byte | 1-byte | 1-byte | 1-byte |
| char, unsigned | | | | | |
| char | | | | | |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| short enum | 2 | 2-byte | 2-byte | 2-byte | 2-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| short, signed | 2 | 2-byte | 2-byte | 2-byte | 2-byte |
| short, | | | | | |
| unsigned short | | | | | |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| int enum | 4 | n.a. | 4-byte | n.a. | n.a. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| int, signed | 4 | 4-byte1 | 4-byte | 4-byte | 4-byte |
| int, unsigned | | | | | |
| int | | | | | |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| long enum | 4 | 4-byte | 4-byte | 4-byte | 4-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| long, signed | 4 | 4-byte | 4-byte | 4-byte | 4-byte |
| long, unsigned | | | | | |
| long | | | | | |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| float | 4 | 4-byte1 | 4-byte | 4-byte | 4-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| double | 8 | 4-byte1 | 8-byte | 4-byte | 8-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| long double4 | 16 or 8 | 4-byte1 | 8-byte | n.a. | 8-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| enum | 4 | 4-byte1 | 4-byte | 4-byte | 4-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | |
| arrays | Same as the alignment rule for the type of the array element. |
| | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| struct | 2,3 | 2-byte1 | 1-, 2-, 4- or | 2- or 4 | 2-, 4- or 8 |
| | | | 8 byte2,3 | byte2,3,5 | byte2,3 |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| union | 2,3 | 2-byte1 | 1-, 2-, 4- or | 2- or 4 | 2-, 4- or 8 |
| | | | 8-byte2,3 | byte2,3,5 | byte2,3 |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | |
| bit-fields | Same alignment as declared type. |
| | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| pointer | 4 | 4-byte | 4-byte | 4-byte | 4-byte |
| | | | | | |
-------------------------------------------------------------------------------------------------------
| | | | | | |
| long pointer | 8 | n.a. | n.a. | n.a. | n.a. |
| | | | | | |
-------------------------------------------------------------------------------------------------------
Table 2-1. Comparison of Data Types and Alignments (continued)
------------------------------------------------------------------------------
| | | | | | |
| Data Type | Size | Alignment| Alignment | Alignment | Alignment |
| | (bytes)| HP C/iX | VAX/VMS C | CCS/C 1000 | CCS/C MPE V |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| char enum | 1 | 1-byte | n.a. | n.a. | n.a. |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| char, signed | 1 | 1-byte | 1-byte | 1-byte | 1-byte |
| char, | | | | | |
| unsigned | | | | | |
| char | | | | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| short enum | 2 | 2-byte | n.a. | n.a. | n.a. |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| short, | 2 | 2-byte | n.a. | n.a. | n.a. |
| signed | | | | | |
| short, | | | | | |
| unsigned | | | | | |
| short | | | | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| int enum | 4 | 4-byte | n.a. | n.a. | n.a. |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| int, signed | 4 | 4-byte | 4-byte | 4-byte | 2-byte (Size |
| int, | | | | | = 2 bytes) |
| unsigned int | | | | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| long enum | 4 | 4-byte | n.a. | n.a. | n.a. |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| long, signed | 4 | 4-byte | 4-byte | 2-byte | 2-byte |
| long, | | | | | |
| unsigned | | | | | |
| long | | | | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| float | 4 | 4-byte | 4-byte | 4-byte | 2-byte |
| | | | (Size = 8 | | |
| | | | bytes) | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| double | 8 | 8-byte | 8-byte | 8-byte | 8-byte |
| | | | (Size = 16 | | |
| | | | bytes) | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| long double4 | 16 or | 8-byte | n.a. | n.a. | n.a. |
| | 8 | | | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| enum | 4 | 4-byte | 4-byte | 2-byte | 2-byte |
| | | | | | |
------------------------------------------------------------------------------
| | |
| arrays | Same as the alignment rule for the type of the |
| | array element. |
| | |
------------------------------------------------------------------------------
| | | | | | |
| struct | 2,3 | 1-, 2-, | 4-byte | 1-, 2-, 4- | 1-, 2-, 4- |
| | | 4- or | | or 8-byte, | or 8-byte, |
| | | 8-byte, | | 2,3 | 2,3 |
| | | 2,3 | | | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| union | 2,3 | 1-, 2-, | 4-byte | 1-, 2-, 4- | 2- or |
| | | 4- or 8- | | or | 4-byte2,3,5 |
| | | byte2,3 | | 8-byte2,3 | |
| | | | | | |
------------------------------------------------------------------------------
| | |
| bit-fields | Same alignment as declared type. |
| | |
------------------------------------------------------------------------------
| | | | | | |
| pointer | 4 | 4-byte | 4-byte | 2-byte | 2-byte (size |
| | | | | (size 2 | 2 bytes) |
| | | | | bytes) | |
| | | | | | |
------------------------------------------------------------------------------
| | | | | | |
| long pointer | 8 | 8-byte | n.a. | n.a. | n.a. |
| | | | | | |
------------------------------------------------------------------------------
Footnotes to Table 2-1
1 Within a structure on HPUX_WORD and MPE_16, all types larger than 2
bytes are aligned on a 2-byte boundary.
2 Alignment is the same as the strictest alignment of any member.
3 Padding is done to a multiple of the alignment size.
4 The long double type is only available in ANSI C and on HP-UX,
MPE/iX, and Natural alignment modes; its size is 16 bytes. On DOMAIN
alignment modes, long double is treated as double with a size of 8
bytes.
5 Series 500 computers align structures on 2 or 4 byte boundaries.
Bit-Field Alignment
With the exception of bit-fields, DOMAIN_WORD structure alignment is the
same as Series 300/400 computers and DOMAIN_NATURAL structure alignment
is the same as Series 700/800 computers.
HP-UX Series 700/800 and MPE/iX use different algorithms than DOMAIN for
aligning bit-fields within a structure. HP-UX Series 700/800 and MPE/iX
align bit-fields, for example f, within a structure by taking the
current_offset prior to f within the structure modulo (%) the
alignment_of_f and adding this result to the width_of_f. The formula for
this algorithm is:
(current_offset % alignment_of_f) + width_of_f
If this exceeds the size of f, then it aligns f according to its type;
otherwise, it allocates it at the offset prior to f. DOMAIN uses a
similar algorithm; however, all of its bit-fields are treated as type
int, regardless of their declared type. For example, consider the
following structure declaration:
struct S {char a:4; char b:3; char c:2;};
Under HP-UX Series 700/800 and MPE/iX, a is at bit offset 0, b is at bit
offset 4, and c is at bit offset 8, not at the next bit 7. The reason is
that bit alignment follows this formula:
(7 % 8) + 2
This results in a value of 9. This exceeds the size of type char (8
bits). So, field c is aligned according to its type (that is, on the
next byte boundary).
The DOMAIN algorithm for aligning bit-fields is the same as HP-UX, except
all bit-fields are treated as type int, regardless of their declared
type. The previous example would look like this using the DOMAIN
algorithm:
(7 % 32) + 2
which results in a value of 9. This does not exceed the size of type int
(32-bits). So, field c is aligned at bit offset 7 as seen in the
following figure.
Figure 2-1. Comparison between HP-UX and DOMAIN Bit Alignments
With respect to size, DOMAIN treats all bit-fields as int. Therefore,
the following struct is allowed:
struct Group_members {
char letter:10;
int number;
} gp_mem;
This same struct declaration would cause an error under HP-UX alignment
rules since 10 bits exceeds the size of the data type char.
NATURAL uses the same scheme for bit-fields as DOMAIN.
MPE/iX 5.0 Documentation