Preface:
Hearing this memory alignment is not feeling very difficult, very difficult to understand, but also likes to test the interviewer
is actually very easy to understand, after reading this article you certainly will understand
First look at two structures
32-bit compiler
int 4 bytes
double 8-byte
short 2 bytes
that no problem
Then the structure sizeof (A) is not a 2 + 4 + 8 = 14
structure 14 B is it?
The result is not, and we think the same
Memory Alignment rules
-
A rule: struct data member or a union of the first offset offset from the start position 0, the starting position of the latter from the other members of the integral type byte size of the start times, such as 4 int byte int then the starting position of the back from the start of a multiple of 4 .
Let's take a appreciated that what exemplified above A
A is the first member of int, from 0 4 bytes, as in FIG.
The second member is a double, accounting for 8 bytes, so his starting position to start from a multiple of 8, currently only use 3 position 8 is so recent
third member is a short, two bytes, starting 2 starting position from the beginning has now used 15 starting position is so 16
The memory arrangement to figure it out later, you say it should not be occupied by a total of 18 bytes 0-17 do
continue to look at Rule number two
- Rule number two: The total size of the structure, size sizeof must be an integer multiple of the largest members of the interior, under-filled
ok largest b 8 bytes, then a and c are filled bars a total of 3 * 8 = 24 bytes
See here above sizeof (A) Why is it understood 24
The problem again if the structure is how do the members of a structure?
The sizeof (c) How much is it?
40
- Rule three: as a member of the structure: a structure if there are some members of the structure, the structure member from the inside maximum element size of an integer multiple of the start address memory (struct a where there struct b, b there char. , int, double and other elements, then b should start from an integer multiple of 8 is stored.) choose maximum storage structural body
Painting at the map
A and b are the first two we should understand, 8 so that the starting position the starting position of the structure 16 according to the three rules body structure is a structure where the largest integer multiple of the largest is the double, rule two according to the structure the maximum total size of the filler is 3 8
So here are a total of 40 0-39
If you see up here to congratulate you all understand the basic memory alignment will understand
Three rules:
- Rule number two: The total size of the structure, size sizeof must be an integer multiple of the largest members of the interior, under-filled
- Rule number two: The total size of the structure, size sizeof must be an integer multiple of the largest members of the interior, under-filled
- Rule three: as a member of the structure: a structure if there are some members of the structure, the structure member from the inside maximum element size of an integer multiple of the start address memory (struct a where there struct b, b there char. , int, double and other elements, then b should start from an integer multiple of 8 is stored.) choose maximum storage structural body
Then talk about #pragma pack ().
Add a #pragma pack (1), you'll be pleased to know that before the code, the code above output
sizeof( A ) = 4+8+2 = 14
This is not ideal in the world do not have memory alignment. Yes, # pragma pack (1), tells the compiler, all times are aligned according to the alignment of the whole 1, in other words, there is no justification rules.
Size C language and C ++ classes and the hollow space of the structure
Predetermined memory space occupied by the structure and the size is 1 byte empty class in C ++, as specified in c ++, any of a variety of objects can not have the same memory address.
In the C language, occupied empty structure in memory size is 0. (gcc test is 0, other compilers may not)
Why memory alignment?
1. Platform reason (transplant reason): Not all hardware platforms can access any data on any address; some hardware platforms can take only certain types of data at a certain address, hardware or throw an exception.
2. For performance reasons: data structures (especially stacks) should be aligned on natural boundaries whenever possible. The reason is that, in order to access non-aligned memory, the processor requires two memory accesses for; aligned access requires only one memory access.
Reference https://blog.csdn.net/hairetz/article/details/4084088