轉載自http://msdn.microsoft.com/en-us/library/ms253949(v=VS.80).aspx
Many CPUs, such as those based on Alpha, IA-64, MIPS, and SuperH architectures, refuse to read misaligned data. When a program requests that one of these CPUs access data that is not aligned, the CPU enters an exception state and notifies the software that it cannot continue. On ARM, MIPS, and SH device platforms, for example, the operating system default is to give the application an exception notification when a misaligned access is requested.
Misaligned memory accesses can incur enormous performance losses on targets that do not support them in hardware.
Alignment
Alignment is a property of a memory address, expressed as the numeric address modulo a power of 2. For example, the address 0x0001103F modulo 4 is 3; that address is said to be aligned to 4n+3, where 4 indicates the chosen power of 2. The alignment of an address depends on the chosen power of two. The same address modulo 8 is 7.
An address is said to be aligned to X if its alignment is Xn+0.
CPUs execute instructions that operate on data stored in memory, and the data are identified by their addresses in memory. In addition to its address, a single datum also has a size. A datum is called naturally aligned if its address is aligned to its size, and misaligned otherwise. For example, an 8-byte floating-point datum is naturally aligned if the address used to identify it is aligned to 8.
Compiler handling of data alignment
Device compilers attempt to allocate data in a way that prevents data misalignment.
For simple data types, the compiler assigns addresses that are multiples of the size in bytes of the data type. Thus, the compiler assigns addresses to variables of type long that are multiples of four, setting the bottom two bits of the address to zero.
In addition, the compiler pads structures in a way that naturally aligns each element of the structure. Consider the structure struct x_ in the following code example:
Copy
struct x_ { char a; // 1 byte int b; // 4 bytes short c; // 2 bytes char d; // 1 byte } MyStruct;
The compiler pads this structure to enforce alignment naturally.
Example
The following code example shows how the compiler places the padded structure in memory:
The compiler pads this structure to enforce alignment naturally.
The following code example shows how the compiler places the padded structure in memory:
Copy
// Shows the actual memory layout struct x_ { char a; // 1 byte char _pad0[3]; // padding to put 'b' on 4-byte boundary int b; // 4 bytes short c; // 2 bytes char d; // 1 byte char _pad1[1]; // padding to make sizeof(x_) multiple of 4 }
Both declarations return sizeof(struct x_) as 12 bytes.
The second declaration includes two padding elements:
The padding aligns the elements of bar[3] in a way that allows natural access.
The following code example shows the bar[3] array layout:
Copy
1: adr
2: offset element
3: ------ -------
4: 0x0000 char a; // bar[0]
5: 0x0001 char pad0[3];
6: 0x0004 int b;
7: 0x0008 short c;
8: 0x000a char d;
9: 0x000b char _pad1[1];
10:
11: 0x000c char a; // bar[1]
12: 0x000d char _pad0[3];
13: 0x0010 int b;
14: 0x0014 short c;
15: 0x0016 char d;
16: 0x0017 char _pad1[1];
17:
18: 0x0018 char a; // bar[2]
19: 0x0019 char _pad0[3];
20: 0x001c int b;
21: 0x0020 short c;
22: 0x0022 char d;
23: 0x0023 char _pad1[1];