HPlogo HP C/HP-UX Programmer's Guide: HP 9000 Computers > Chapter 2 Storage and Alignment Comparisons

Aligning Structures Between Architectures

» 

Technical documentation

Complete book in PDF

 » Table of Contents

Differences in data type alignment can cause problems when porting code or data between systems that have different alignment schemes. For example, if you write a C program on the Series 300/400 that writes records to a file, then read the file using the same program on HP 9000 workstations and servers, it may not work properly because the data may fall on different byte boundaries within the file because of alignment differences.

Three methods can be used for aligning data within structures so that it can be shared between different architectures.

  • Use only ASCII formatted data. This is the safest method, but may have negative performance and space implications.

  • Use the HP_ALIGN and PACK pragmas, to force a particular alignment scheme, regardless of the architecture on which it is used. See “The HP_ALIGN Pragma” and “The PACK Pragma” for details.

  • Define platform independent data structures using explicit padding.

To illustrate the portability problem raised by different alignments, consider the following example.

#include <stdio.h>
struct char_int
{
char field1;
int field2;
};
main (void)
{
FILE *fp;
struct char_int s;
.
.
.
fp = fopen("myfile", "w");
fwrite(&s, sizeof(s), 1, fp);
.
.
.
}

The alignment for the struct that is written to myfile in the above example is shown in Figure 2-7 “Comparison of HPUX_WORD and HPUX_NATURAL Byte Alignments ”.

Figure 2-7 Comparison of HPUX_WORD and HPUX_NATURAL Byte Alignments

Comparison of HPUX_WORD and HPUX_NATURAL Byte Alignments

In the HPUX_WORD alignment mode, six bytes are written to myfile. The integer field2 begins on the third byte. In the HPUX_NATURAL alignment mode, eight bytes are written to myfile. The integer field2 begins on the fifth byte.

Examples of Structure Alignment on Different Systems

The code fragment below can be used to illustrate 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};

HP C/HP-UX 9000 Workstations and Servers and HP C/iX

Figure 2-8 “Storage with HP C on the HP 9000 workstations and servers and HP 3000 Series 900 ” shows how the data in the above example is stored in memory when using HP C on the HP 9000 workstations and servers and HP 3000 Series 900. The values are shown above the variable names. Shaded cells indicate padding bytes.

Figure 2-8 Storage with HP C on the HP 9000 workstations and servers and HP 3000 Series 900

Storage with HP C on the HP 9000 workstations and servers 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-7 “Padding on HP 9000 Workstations and Servers and HP 3000 Series 900 ” shows the padding for the example code fragment:

Table 2-7 Padding on HP 9000 Workstations and Servers 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 workstations and servers and HP 3000 Series 900 are:

  • On the Series 300/400, a structure is aligned on a 2-byte boundary. On the HP 9000 workstations and servers 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 workstations and servers 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 workstations and servers 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 workstations and servers 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-9 “Storage with HP C on the HP 9000 Series 300/400 ”:

Figure 2-9 Storage with HP C on the HP 9000 Series 300/400

Storage with HP C on the HP 9000 Series 300/400

Table 2-8 “Padding on the HP 9000 Series 300/400” shows the padding for the example code fragment.

Table 2-8 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-10 “Storage with CCS/C” shows how the members of the structure defined in “Examples of Structure Alignment on Different Systems ” are aligned in memory when using CCS/C on the HP 1000 or HP 3000:

Figure 2-10 Storage with CCS/C

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-9 “Padding with CCS/C ” shows the padding for the example code fragment:

Table 2-9 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 workstations and servers, 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 “Examples of Structure Alignment on Different Systems ” is stored as shown in Figure 2-11 “Storage on VAX/VMS C ”:

Figure 2-11 Storage on VAX/VMS C

Storage on VAX/VMS C

Table 2-10 “Padding on VAX/VMS C ” shows the padding for the example code fragment

Table 2-10 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.

 

© Hewlett-Packard Development Company, L.P.