Data Alignment Summary

Source: Internet
Author: User

It is important to note: What is the alignment? byte Alignmentnot how many bits are aligned.

Justification Reason:

such as slices, memory is generally four units a column, the CPU reads the memory data, the data of each unit is read in parallel by the bus. For registers of CPU 32bit.
    • 0-7bit is a bit from the memory chip 0
    • 8-15bit is a bit from memory chip 1
    • 16-23bit is from Chip 2
    • 24-31 from Chip 3
The memory structure is that the memory address is a unit that is the base unit is 1 bytes, that is, the 8-bit binary number. When reading the data is actually 0-7 bits from the chip 0 read, 8 to 15 bits read from the chip 2, and so on. So read a 4 byte data, is four chips simultaneously to register inside reads. For example, the left side is the address offset. For the bus efficient transmission address, the number on the chip is the memory address, each memory address needs to read several, on this chip to read back several. Note that this is the physical address, not the virtual address, the physical address is not linear, is chaotic.   If the 4-bit int is read, the 0--7bit of the 8bits fill register is read from chip 0, the chip 1 reads the 8bits fill 8--15bits, and so on.   If the data is not aligned, to read 4 bytes from address 1, read in, you need to shift the CPU to the left 8bits to achieve the correct. But after the CPU reads the data, it needs to send the address to the memory, if there is no alignment, the amount of the address offset (the left address offset) is different, the 1, 2, 3 address offset is 0, and the 0 chip accepts the address offset is 1, so that the sending address can not use 1 bus to complete, but requires 4 bus , each address bus requires 32 CPU pins, and 32bitCPU totals only 400 pins, so the design adds complexity to the hardware design and is unnecessary. Without changing the design complexity, accessing unaligned data requires two accesses to get the data. For example: The access address is 1, 2, 3, 4 of the 4 bytes of data, the first to read the data of address 0-3, in reading 4--7 data, and then 1--4 the data out to put into the target register. Therefore, it is not necessary to access memory multiple times. Some CPUs such as power PCs directly prohibit access to their data.   Summarize why data alignment is required. In order to increase the bandwidth of CPU read data, the memory system usually adopts parallel structure, which can transmit data in parallel. Such a parallel structure makes access to aligned data fast, but it is not possible to make the interface between the CPU and the memory system more complicated by the fact that access is not as fast as the odd data. After the tradeoff, the end result is that access to aligned data is fast, access is not odd data is slow (requires 2 access) or simply prohibit access to the wrong odd data.   Alignment is that the address must be divisible by an integer, which is the alignment parameter. The legal value ranges for alignment are: 1, 2, 4, 8, 、。。。 , 8192  for basic types (excluding structs, unions, and arrays) the number of Zimo for different data types is usually equal to the size of the data type, such as the number of Zimo to 2,int for Zimo of Zimo to 1,short for Char, and the 4,float of the number Zimo is The number of Zimo for 4,double is 8 whenHowever, this may vary for each compiler of Zimo and can be set. You can get #pragma settings in Visual C + + with the/zp option.   Combined, the basic type of Zimo number is the smaller of the size of this type and the alignment limit set. For structs, unions, and arrays, the number of Zimo is the maximum number of Zimo for its members.   Structure Alignment:__declspec (align (#)) and #pragma pack (n) one, #pragma pack (n)#pragma pack (+) typedef struct {int A;Char b;double C;}test;A data member in a struct, except that the first is always at the beginning, the address of the other data member must be a multiple of the smaller of its own size or alignment parameters. The address of the data member of the struct must be the size of itself and the smaller of the alignment parameters.  a struct consisting of simple types, the size of which is determined by the address of each member variable. By defining the order in which the address begins, each variable takes min (alignment parameter, sizeof (type)) to determine how many times the starting address is, and then fills the data and finally the total size, starting at address 0. When the pack is equal to 2, the final size is a multiple of 2, which needs to be taken up to a multiple of 2. Pack of 1 is not required.  For structures containing structures, the method is the same as above, the parameters of the struct variables are min (pack alignment parameter, struct member maximum element size). __declspec (align (#))When a variable or struct is affected by both __declspec (align (#)) and #pragma pack (n), the former has a higher precedence. Each member's Addressis a multiple of the former N or a multiple of the latter m or a multiple of the member size. That min (n,m,sizeof (member variable type));The final structure of the body sizeEither a multiple of M in __declspec (align (M)), or a multiple of the maximum type size in the struct, to the maximum. That Max (m,sizeof (maximum type size));Examples of structures in a struct:
#include <stdio.h>#include"stdafx.h"#include<stdlib.h>//using namespace std;#pragmaPack (push, 4)__declspec (align ( +) )structd{intI1; DoubleD1; intI2; inti3;}; __declspec (align ( -) )structe{intI1;     D M_d; inti2;};intMain () {cout<<"sizeof (int) ="<<sizeof(int) <<Endl; cout<<"sizeof (char) ="<<sizeof(Char) <<Endl; cout<<"sizeof (double) ="<<sizeof(Double) <<Endl; cout<<sizeof(D) <<Endl; cout<<sizeof(E) <<Endl; System ("PAUSE"); return 0;}
The result is that sizeof (E) is 96, why is this result? For struct E, the first element is of type int, so it occupies the [0~3] address cell. The second element is a struct, which has a high priority because it is affected by the above __declspec (align (32)), so the starting address is a multiple of 32 and the size is 32B, which should be placed at the [32~63] cell. The last one is a variable of type int, size 4, so it should be a multiple of 4 with the address [64~67]. Therefore, the size of the structure e should be from [0~67], Occupy 68B, and because there is a limit __declspec (align (16)), and the maximum offset of the member variable is sizeof (D) = 32, so we finally the structure of the size should be a multiple of their maximum value, That is, a multiple of 32, 68 up to 32 should be a multiple of 96. The result is 96. Note: One feature of _declspec (Align ()) is that It only specifies the location of the data alignment, and does not specify the length of memory that the data actually occupies, after the specified data is placed in a determined location, the data fill that follows is still populated as specified by the #pragma pack. Special Note: do not forget that __declspec (align (n)) aligns the first address n bytes of the struct.

Data Alignment Summary

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: 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.