Data Alignment Mechanism

Source: Internet
Author: User

Reprinted from http://msdn.microsoft.com/en-us/library/ms253949 (v = VS.80). aspx

Using CPUs, such as those based on Alpha, IA-64, MIPS, and SuperH using tures, 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 isXn + 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 typeLongThat 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 structureStruct 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.

Example

The following code example shows how the compiler places the padded structure in memory:

Copy

// Shows the actual memory layoutstruct 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 returnSizeof (struct x _)As 12 bytes.

The second declaration includes two padding elements:

  • Char _ pad0 [3]To alignIntB member on a four-byte boundary
  • Char _ pad1 [1]To align the array elements of the structureStruct _ x bar [3];

The padding aligns the elementsBar [3] In a way that allows natural access.

The following code example showsBar [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];
See AlsoReference _ unaligned keywordConceptsWorking with Packing Structures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.