Next | Prev | Up | Top | Contents | Index

Structure and Union Layout Examples

Simple examples illustrate the alignment and size issues of C structures and unions.


Example 1: Structure Smaller Than a Word

struct c {
   char c;
} c1;
Byte-aligned, sizeof struct is 1.

Figure 2-1 : Structure Smaller Than a Word


Example 2: Structure With No Padding

struct s {
   char c;
   char d;
   short s;
   int   i;
} s1;
Word-aligned, sizeof struct is 8.

Figure 2-2 : Structure With No Padding


Example 3: Structure With Internal Padding

struct t {
   char c;
   char d;
   short s;
   long  l;
} t1;

Figure 2-3 : Structure With Internal Padding


Example 4: Structure With Internal and Tail Padding

struct l {
   char c;
   long  l;
   short s;
} l1;

Figure 2-4 : Structure With Internal and Tail Padding


Example 5: Union Allocation

union u {
   char c;
   short s;
   int  i;
   long  l;
} u1;

Figure 2-5 : Union Allocation


Next | Prev | Up | Top | Contents | Index