Specifications

Red Hat Enterprise Linux to Oracle Solaris Porting Guide
18
The processor will have to issue two different read instructions to read the complete integer. Thus, it
takes twice as long to read a misaligned integer. In short, the addresses of memory chunks should be in
multiples of their sizes. If an address satisfies this requirement, it is said to be properly aligned. The
consequences of accessing data via an unaligned address can range from slower execution to program
termination.
Figure 3-2. An improperly aligned integer value would require two fetch operations.
Data Structures and Sizes
In order to generate efficient code, compilers have to follow the byte alignment restrictions defined by
the target processors. This means that compilers have to add pad bytes into user-defined structures so
that the structure does not violate any restrictions imposed by the target processor. The compiler
padding is illustrated in the following example. Here, an int is assumed to be 4 bytes, a short is 2
bytes, and a char is a single byte.
struct mydata {
char C;
long L;
short B;
long J;
};
Since the alignment of an int on this platform is 4 bytes, 3 bytes are added after char c, and two
bytes are added at the end of short b, as shown in Figure 3-3. The size of this struct is 16 bytes
on an x86 system and 32 bytes on a SPARC (64-bit) system. By padding, the address of this
struct
data is divisible evenly by 4. This is called structure member alignment. Of course, the size of
struct
has grown as a consequence.