This problem is indeed quite basic. In order to give yourself a summary, this article is also a note record and strives to be the most streamlined.
Structure alignment keywords:
Alignment coefficient (also called alignment modulus): This coefficient is changed using the pre-compiled command # pragma pack (N), n =, 16.
"Alignment rules": The data members are aligned in two steps, and the overall alignment is as follows:
1: Data member alignment: perform the following operations according to the value specified by # pragma pack and the smaller value in the length of the data member.
Formula: min (n, sizeof (curr_mem ))
2: The overall alignment is performed according to the value specified by # pragma pack and the smaller value in the maximum data member length of the structure (or union ).
Formula: min (n, max (sizeof (mem1), sizeof (MEM2), sizeof (mem3 )......))
Example:
2-byte alignment (# pragma pack (2 ))
Result: sizeof (struct test_t) = 10
Analysis process:
Data member alignment:
# Pragma pack (2) structTest_t {int A;/* length 4> 2 aligned by 2; Start offset = 0; storage location range [0, 3] */Char B; /* length 1 <2 aligned by 1; Start offset = 4; storage location range [4] */short C;/* length 2 = 2 aligned by 2; start offset = 6; storage location range [6, 7] */Char D;/* length 1 <2 aligned by 1; Start offset = 8; storage location range [8] */}; # Pragma pack ()
Overall alignment:
Alignment according to min (2, (max (INT, short, char) Results
The starting address in the memory pool is aligned:
# Define ngx_alignment sizeof (unsigned long)/* platform word */
# Define ngx_align_ptr (P, )\
(U_char *) (uintptr_t) (p) + (uintptr_t) A-1 ))&~ (Uintptr_t) A-1 ))
Call: ngx_align_ptr (PTR, ngx_alignment)
The detailed process is not detailed. There are several key points:
1: role of "word" in computer systems (Multi-read in-depth understanding of computers)
2: currently on Linux, the pointer type is the same as that of unsigned long.