Calculating the Size of Structs
Category Programming Techniques
Calculating the size of structs in the simplest and most understandable way.
Struct size calculation must follow the byte alignment principle.
The default byte alignment for structs generally adheres to three guidelines:
1) The starting address of the struct variable must be divisible by the size of its widest basic type member;
2) The offset of each member relative to the start of the struct must be an integer multiple of the member's size. If necessary, the compiler will add padding bytes between members;
3) The total size of the struct must be an integer multiple of the size of its widest basic type member. If necessary, the compiler will add padding bytes after the last member.
>
For now, just focus on the third guideline, which states that the result of the struct size must be an integer multiple of the largest byte in the members.
Consider the following two struct definitions:
struct { char a; short b; char c; }S1;
struct { char a; char b; short c; }S2;
Testing with a program yields sizeof(S1)=6 , sizeof(S2)=4.
Note: Why does changing the order of struct members result in different sizes?
Explanation:
(1) First, identify the largest byte among the member variables. For S1 and S2, the largest is short, which is 2 bytes;
(2) Therefore, align everything to 2 bytes, meaning that any additional space will be filled with padding bytes. Each box in the diagram represents one byte;
(3) Start with 2 boxes and add according to the member order, incrementing by 2 each time
char->short->char
:
For S1, the struct size is 2*3=6. The extra byte for the second char is not discarded because it adheres to the third guideline, which states that the struct size must be an integer multiple of the largest byte in the members.
S1=2*3=6
For S2, draw a diagram with the order char->char->short
:
S2=2*2=4
Using this method, consider this struct:
struct stu1
{
int i;
char c;
int j;
};
Clearly, the largest byte is 4. The order is int char int:
Since int occupies 4 bytes and char has already taken one, the remaining three bytes are used for padding.
Stu1=3*4=12
What if the order is changed?
struct stu2
{
int i;
int j;
char c;
};
Stu2=3*4=12
Now, consider a struct where a member is another struct: Simply add the size of the nested struct as a whole.
typedef struct A
{
char a1;
short int a2;
int a3;
double d;
};
A=16
typedef struct B
{
long int b2;
short int b1;
A a;
};
For B, ignore A a first, and calculate the size of struct B as 8. Therefore, the final result is 8+16=24; 24 is the final size.
>
Original article: https://www.cnblogs.com/lykbk/archive/2013/04/02/krtmbhrkhoirtj9468945.html
#
-
** Song Song
* 105**[email protected]
The third guideline is incorrect, stating that "the result of the struct size must be an integer multiple of the largest byte in the members."
Correctly, if no alignment byte size is set, the largest member is the alignment byte size.
If an alignment byte size is set, the alignment byte size is: min(largest member, set alignment byte size).
Reference article: https://zhuanlan.zhihu.com/p/30007037
** Song Song
* 105**[email protected]
** Click to Share Notes
-
-
-