0
Follow
0
View

On the Problem of Calculating Bytes in Struct Common Body(Language-C)

cxfchw 注册会员
2023-02-25 22:11

is the cause of byte alignment
4-byte alignment, which takes up bytes of itself
4
1
4
5
but 4-byte alignment, the 1 in the middle actually takes up 4 bytes, The last 5 takes up 8 bytes, so 20 bytes in total

cuitie88 注册会员
2023-02-25 22:11

This answer partially references GPT, GPT_Pro to solve the problem better.
Because the structure has byte alignment principle, when calculating the size of the structure, The calculations need to be byte aligned. Specifically, you take the largest member variable in the structure as the alignment criterion, and then calculate how many bytes the other member variables need to fill so that the overall structure size always increases by a multiple of the largest member variable.

For example: In the example above, the largest member variable is float, float takes 4 bytes, so the byte size of this structure must be a multiple of 4, that is, the common body takes 5 bytes, int takes 4 bytes, char takes 1 byte, float takes 4 bytes, so these member variables will take 14 bytes, Because of byte alignment, the 14 bytes fill a multiple of float, so the final size of the structure should be 20 bytes.

darren895 注册会员
2023-02-25 22:11
< div class = "md_content_show e397 data - v - 3967" = "" >

thank you

debbug 注册会员

Publish Time
2023-02-25 22:11
Update Time
2023-02-25 22:11