HP 3000 Manuals

Alignment within Structures [ HP C Programmer's Guide ] MPE/iX 5.0 Documentation


HP C Programmer's Guide

Alignment within Structures 

In a structure, each member is allocated sequentially at the next
alignment boundary corresponding to its type.  Therefore, the structure
might be padded internally if its members' types have different alignment
requirements.  In a union, all members are allocated starting at the same
memory location.  Both structures and unions can have padding at the end,
in order to make the size a multiple of the alignment.

The code fragment in Figure 2-2  illustrates the alignment on various
systems.
______________________________________________________________
|                                                            |
|     struct x {                                             |
|        char y[3];                                          |
|        short z;                                            |
|        char w[5];                                          |
|     };                                                     |
|                                                            |
|     struct q {                                             |
|        char n;                                             |
|        struct x v[2];                                      |
|        double u;                                           |
|        char t;                                             |
|        int s:6;                                            |
|        char m;                                             |
|     } a = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,|
|            20.0,21,22,23};                                 |
______________________________________________________________

          Figure 2-2.  Code Fragment for Comparing Storage and Alignment 

HP C/HP-UX Series 700/800 and HP C/iX 

Figure 2-3 shows how the data in Figure 2-2  is stored in memory when
using HP C on the HP 9000 Series 700/800 and HP 3000 Series 900.  The
values are shown above the variable names.  Shaded cells indicate padding
bytes.

	       Click here to view figure.
          Figure 2-3.  Storage with HP C on the HP 9000 Series 700/800 and HP 3000 Series 900 

The struct q is aligned on an 8-byte boundary because the most
restrictive data type within the structure is the double u.

Table 2-2  shows the padding for the example code fragment:

          Table 2-2.  Padding on HP 9000 Series 700/800 and HP 3000 Series 900 
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| Padding Location  |                          Reason for Padding                           |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+1               | The most restrictive type of the structure x is short; therefore, the |
|                   | structure is 2-byte aligned.                                          |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+5               | Aligns the short z on a 2-byte boundary.                              |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+13              | Fills out the struct x to a 2-byte boundary.                          |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+17              | Aligns the short z on a 2-byte boundary.                              |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+25              | Fills out the structure to a 2-byte boundary.                         |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+26 through a+31 | Aligns the double u on an 8-byte boundary.  The bit-field s begins    |
|                   | immediately after the previous item at a+41.  Two bits of padding is  |
|                   | necessary to align the next byte properly.                            |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+43 through a+47 | Fills out the struct q to an 8-byte boundary.                         |
|                   |                                                                       |
---------------------------------------------------------------------------------------------

HP C on the Series 300/400 

The differences between HP C on the HP 9000 Series 300/400 and HP C on
the HP 9000 Series 700/800 and HP 3000 Series 900 are:

   *   On the Series 300/400, a structure is aligned on a 2-byte
       boundary.  On the HP 9000 Series 700/800 and HP 3000 Series 900,
       it is aligned according to the most restrictive data type within
       the structure.

   *   On the Series 300/400, the double data type is 2-byte aligned
       within structures.  It is 8-byte aligned on the HP 9000 Series
       700/800 and HP 3000 Series 900.

   *   On the Series 300/400, the long double, available in ANSI mode
       only, is 2-byte aligned within structures.  The long double is
       8-byte aligned on the HP 9000 Series 700/800 and HP 3000 Series
       900.

   *   On the Series 300/400, the enumerated data type is 2-byte aligned
       in a structure, array, or union.  The enumerated type is always
       4-byte aligned on the HP 9000 Series 700/800 and HP 3000 Series
       900, unless a sized enumeration is used.

When the sample code fragment is compiled and run, the data is stored as
shown in Figure 2-4 :

	       Click here to view figure.
          Figure 2-4.  Storage with HP C on the HP 9000 Series 300/400 

Table 2-3  shows the padding for the example code fragment:

          Table 2-3.  Padding on the HP 9000 Series 300/400 

---------------------------------------------------------------------------------------------
|                   |                                                                       |
| Padding Location  |                          Reason For Padding                           |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+1               | Within structures align short on a 2-byte boundary.                   |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+5               | Aligns the short z on a 2-byte boundary.                              |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+14              | Structures within structures are aligned on a 2-byte boundary.        |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+17              | Aligns the short z on a 2-byte boundary.                              |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+25              | Doubles are 2-byte aligned within structures.                         |
|                   |                                                                       |
---------------------------------------------------------------------------------------------
|                   |                                                                       |
| a+37              | Pads a to a 2-byte boundary.                                          |
|                   |                                                                       |
---------------------------------------------------------------------------------------------

CCS/C on the HP 1000 and HP 3000 

Figure 2-5 shows how the members of the structure defined in Figure 2-2
are aligned in memory when using CCS/C on the HP 1000 or HP 3000:

	       Click here to view figure.
          Figure 2-5.  Storage with CCS/C 


NOTE All data types and structures are 2-byte aligned when using CCS/C on the HP 1000 or HP 3000.
Table 2-4 shows the padding for the example code fragment: Table 2-4. Padding with CCS/C --------------------------------------------------------------------------------------------- | | | | Padding Location | Reason for Padding | | | | --------------------------------------------------------------------------------------------- | | | | a+1 | Aligns the structure on a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+5 | Aligns the short z on a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+13 | Fills out the struct x to a 2-byte boundary. (Aligns the character | | | on a 2-byte boundary.) | | | | --------------------------------------------------------------------------------------------- | | | | a+17 | Aligns the short z on a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+25 | Fills out the structure to a 2-byte boundary and aligns the double u | | | on a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+37 | Pads a to a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- VAX/VMS C The differences between HP C and VAX/VMS C are: * In HP C Series 700/800, the double type is 8-byte aligned; in VAX/VMS C, the double type is 4-byte aligned. * In HP C, bit-fields are packed from left to right. In VAX/VMS C, the fields are packed from right to left. * HP C uses big-endian data storage with the most significant byte on the left. VAX/VMS C uses little-endian data storage with the most significant byte on the right. (See the swab function in the HP-UX Reference manual for information about converting from little-endian to big-endian.) In VAX/VMS C, the data from the program in Figure 2-2 is stored as shown in Figure 2-6: Click here to view figure. Figure 2-6. Storage on VAX/VMS C Table 2-5 shows the padding for the example code fragment: Table 2-5. Padding on VAX/VMS C --------------------------------------------------------------------------------------------- | | | | Padding Location | Reason for Padding | | | | --------------------------------------------------------------------------------------------- | | | | a+1 | The most restrictive type of any struct x member is short; therefore, | | | struct x is 2-byte aligned. | | | | --------------------------------------------------------------------------------------------- | | | | a+5 | Aligns the short z on a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+13 | Fills out the struct x to a 2-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+17 | Needed for alignment of the short z. | | | | --------------------------------------------------------------------------------------------- | | | | a+25 through a+27 | Fills out the structure to a 2-byte boundary and aligns the double u | | | on a 4-byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+37 | Aligns the char m on a byte boundary. | | | | --------------------------------------------------------------------------------------------- | | | | a+39 | Fills out the structure to a 4-byte boundary. | | | | ---------------------------------------------------------------------------------------------


MPE/iX 5.0 Documentation