当前位置:网站首页>Structure size calculation of C language struct

Structure size calculation of C language struct

2022-06-24 03:53:00 Good pie notes

Go straight to the topic , To judge the space occupied by a structure , Generally speaking, it is divided into three steps :

1. First determine the actual alignment unit , It is determined by three factors

    1> CPU cycle

    WIN  vs  qt   Default 8 Byte alignment

    Linux 32 position Default 4 Byte alignment ,64 Bit default 8 Byte alignment

    2> The largest member of the structure ( Basic data type variable )

    3> Precompiling instructions #pragma pack(n) Manual settings     n-- It can only be filled in 1 2 4 8 16

    The smallest of the above three , Is the actual alignment unit ( there “ Actual alignment units ” It is for the convenience of me to distinguish the concept of casually taking )

2. Except for the first member of the structure , The address of all other members is relative to the structure address ( The address of its first member ) The offset of must be Actual alignment units Or an integral multiple of its own size ( Take the smaller of the two )

3. The overall size of the structure must be Actual alignment units Integer multiple .

The above three steps are the universal formula , Let's look at a practical example (linux 64 Under the system ):

1.

nums in , The alignment unit is not set manually ,linux64 The default alignment unit of the system is 8 byte , Structure nums Maximum member of double d Occupy 8 Bytes , Therefore, the actual alignment byte is the smallest of the two , namely 8 byte .

char a At the starting address of the structure ;

short b Occupy 2 Bytes ,2 Less than the actual aligned bytes 8, so b The starting address of is relative to a The offset of the start address of must be 2 An integer multiple of bytes ;

int c Occupy 4 Bytes ,4 Less than the actual aligned bytes 8, so c  The starting address is relative to a The offset of the start address of must be 4 An integer multiple of bytes ;

double d Occupy 8 Bytes ,8 Align bytes with actual 8 equal , so d The starting address of is relative to a The offset of the start address of must be 8 An integer multiple of bytes ;

therefore nums The space occupied is as follows :    1(a)+1( Wasted space , from b The starting address of determines this 1 Bytes must be vacated )+2(b)+4(c)+8(d)=16 Bytes

2. Add a... At the end of the structure char Array , Let's see :

To member double d until , Structure nums The space occupied is 16, It has been analyzed above , Then there is a char Type of the array , The type of array is char[13], It's not a basic data type , It is still regarded as 13 individual char Type variable to handle ,char Occupy 1 Bytes , Less than the actual aligned bytes 8, So this 13 individual char Type variables can be directly next to double d Back placed ( The final result looks like the entire array is next to double d place ); So the general space situation is :1(a)+1( Waste space )+2(b)+4(c)+8(d)+13(arr)=29; but 29 Not satisfied with the last step of the above three steps :“ The size of the entire structure must be an integral multiple of the actual alignment unit ”, therefore 29+5( Waste space )=32, So finally nums The spatial situation of is 1(a)+1( Waste space )+2(b)+4(c)+8(d)+13(arr)+5( Waste space )=32 byte

In addition, the byte alignment of nested structures is the same as the above principle , The only thing to note is that the distance between the starting address of the substructure and the starting address of the parent structure must be the largest member of the substructure or the actual alignment unit ( Or take the smaller one ) Integer multiple .

原网站

版权声明
本文为[Good pie notes]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/09/20210917190323838z.html