Unaligned access structure

Unaligned access structure
1, the access elements in the nature or structure is actually a pointer, the bonding type of the whole structure of this element and offset this element to be accessed.
2, accounting for the actual number of bytes in each element of the type and number of bytes occupied by itself is not necessarily exactly the same. (Char c actual number of bytes for example may be an account, it could be)
2, 3 may be, it may be 4)
3, in general, we use. Ways to access structure elements, we do not consider elements aligned structure. Because the compiler will help us deal with this detail.
But the C language itself is a very low-level language, and often need to do embedded development from the perspective of memory, as a pointer to deal with the structure and the elements, so they still need to know
the structure alignment rules.
4, the structure elements are aligned to coincide with the main access hardware, which means that the hardware itself has physical limitations, if the alignment arrangement will improve efficiency and access, otherwise it
will greatly reduce efficiency.
5, memory itself is a physical device (DDR memory chips, DDR controller of the SoC), itself has some limitations: 4-byte access its memory if each access,
then the efficiency is the highest; if not align access efficiency is much lower.
6, aligned accesses the expense of memory space, in exchange for speed performance; rather than aligned accesses the expense of access speed performance, in exchange for full use of memory space.
7, the rule compiler itself may be provided aligned memory, has to keep in mind the following rules:
First: 32-bit compiler, the compiler typically default alignment is 4 byte alignment.
8, analysis of critical points and alignment structures:
(1) the structure aligned to be considered: the entire structure itself must be positioned at a 4 byte alignment, the size of the structure must be aligned in multiples of 4. (Compiler to 8-byte aligned, where 4 is 8).
(2) the structure of each storage element itself must be aligned, and each element has its own alignment rule itself.
(3) The compiler considering storage structure, in order to meet the above two points needs minimum required memory arranged to count.
Exemplified:
struct mystruct1
{// 4-byte alignment byte alignment. 1
 int A; // 4 4
 char B; // 2. 1 (+. 1. 1). 1 one is filled
 Short C; 2 // 2
}
analysis :
first, the whole structure, the whole structure variable is a 4-byte alignment is guaranteed by the compiler, we do not have to worry about.
(1) and a first element, a start address of the start address is the overall structure, the 4-byte naturally aligned, but the end address is a final say by the next element.
(2) and b is the second element, because the element itself, a 4 bytes, are aligned itself, so leaving the starting address b is 4 byte alignment address, it can be directly put b
(b It determines the position of a total of 4 bytes, since no filling).
(3) b after the set start address, the end address can not be determined (as may be required to fill), the end address to set the next element to see.
(4) and the third element is c, short type requires 2 bytes alignment (short address type element must be placed at such a similar 0,2,4,8, 3 can not be placed such
odd address), the c-b can not be stored next, a solution is to add padding bytes after the b, c and then stored starting. After c is not over is done, because the whole structure
Also the size is an integer multiple of 4 may also be required to fill may not be required.
9, the structure is provided instruction alignment in bits: __ __ attribute ((packed The)) __attribute __ ((the aligned (n-)))
                            #pragma Pack ();. 1 byte alignment #pragma pack (4); 4-byte alignment

Guess you like

Origin www.cnblogs.com/jiangtongxue/p/11695317.html