Byte alignment is one of the most frequently learned points in an interview written exam.
There are only a few steps you can take to calculate the size of a struct.
1. Confirm the length of all members in the struct
This table can be referenced.
|
|
Char |
Short |
Int |
Long |
Float |
Double |
Long Long |
Long double |
Win-32 |
Length |
1 |
2 |
4 |
4 |
4 |
8 |
8 |
8 |
Modulus |
1 |
2 |
4 |
4 |
4 |
8 |
8 |
8 |
Linux-32 |
Length |
1 |
2 |
4 |
4 |
4 |
8 |
8 |
12 |
Modulus |
1 |
2 |
4 |
4 |
4 |
4 |
4 |
4 |
Linux-64 |
Length |
1 |
2 |
4 |
8 |
4 |
8 |
8 |
16 |
2. Determine the default number of Zimo for the system:
"#pragma pack specified value ", " #pragma pack not specified , default is 8 in VC6 "
In the following form, you specify the number of Zimo.
#pragma pack (2)
struct MY_STRUCT
{
Char A;
a long double b;
};
#pragma pack ()
3. A set of numbers and a number (the size of each member and the modulus of the system) is obtained at this point, and the total length is calculated by the following method length=0
1. Take out the length of the first member and add it to the total length. + + sizeof (MEMBER1) because there is no alignment problem for the first member starting from 0
2. Take out the length of the second member, and compare the system modulus to take a smaller number of min
3. Move behind the previous member so that the address after the byte is divisible by Min, and the second member's location is the address.
4. etc.
4. Finally take out the maximum length of all members Max and the system modulus comparison, come up with a smaller totalmin, see the total length of the current can be totalmin divisible, if possible now, if not to add n let the total length can be totalmin divisible by the end is the final size.
As an example:
struct MY_STRUCT
{
Char A;
Double b;
};
First step: Get the length of all members: a length of 1 B is 8
Step two: Get the module of the system at this time no pack is specified so the default in VC6 is 8
Step three: Put a into the total length is now 1, the memory layout for [0] 0 location is a and then take out the length of B 8 and the default 8 comparison, remove the smaller is 8, at which point B should be divisible by 8
But we look at a is the length of a from 0 onwards is 1 so the address must be moved backwards 7, so that the starting address of B is 8 to be guaranteed to be divisible by 8. So add 7 slots between A and B, and let B start with 8.
[0] [1 2 3 4 5 6 7] [8 9 10 11 12 13 14 15]
Fourth: Total length is a + b + null = 1 + 8 + 7 = 16
Take out all the members of the longest B that's 8 out of the system default 8 take the smaller 8
See if 16 can be divisible by 8 here, so the last length is 16.
Here's a more complicated one:
struct MY_STRUCT
{
int A;
Char b;
Double C;
int D;
Char e;
};
or follow those steps:
1. Get all members lengths of 4 1 8 4 1, respectively
2. Get system modulus There's no custom pack, so it's 8.
3. First member a occupies from 0 position [0123] four bytes
Then the turn is B. Since a occupies 0123, B starts from 4 and the length of B is 1, and the system modulus is 8, and the small 1 is removed. 4 can be divisible by 1 without filling the vacancy at this time [0123][4] 0123 is placed a 4 with B
Then the turn C, C is 8 and the system modulus ratio to take out a small 8, 01234 have something, C to start from 5, but 5 can not be divisible by 8 to be divisible by 8, at least at the beginning of 8 this position so 567 storage space, C starting from 8 at this time [0 1 2 3] [4] [5 6 7] 0 11 12 13 14 15]
Now it's D, D is 4 system mode is 8 take out small 4 start address 16 can be divisible by 4, so D is starting from 16 [0 1 2 3] [4] [5 6 7] [8 9 10 11 12 13 14 15][16 17 18 19]
Finally, E, 1 and 8 are divisible by 1 from 1 starting addresses, so the final structure is [0 1 2 3] [4] [5 6 7] [8 9 10 11 12 13 14 15][16 17 18 19][20]
4. Finally take out the longest of all members is the C length of 8 and the system modulus 8 is smaller than the removal of 8
The total length of the current 21 cannot be divisible by 8, so we still have to back up 3 bits [0 1 2 3] [4] [5 6 7] [8 9 10 11 12 13 14 15][16 17 18 19][20][21 22 23]
Altogether 24 bytes.
Attention:
With pointer members, the size is the size of the pointer.
Note: About the size of 64-bit systems and 32-bit systems
It can be determined that the 32-bit system has a pointer size of 32 bits, which is 4 bytes.
A 64-bit system has a pointer size of 64 bits or 8 bytes.
However, for types of type long double, the 32-bit 64-bit is different depending on the system size. No set criteria
However, if int32_t or int64_t are present, these are types defined for portability, representing 32-bit int and 64-bit int with fixed 32-bit and 64-bit lengths, respectively.
"C + +" byte alignment, which is the actual size of a struct