当前位置:网站首页>How big is the empty structure?

How big is the empty structure?

2022-07-06 22:31:00 It's Beichen not too PI acridine

How big is the empty structure ?

struct keyword

struct It's a magic keyword , It packages some associated data into a whole , Easy to use .

In the network protocol 、 Communication control 、 Embedded system 、 Drive development and other places , It's not a simple byte stream that we often have to transfer (char Type of the array ), It's a combination of data , Its manifestation is a structure . Inexperienced developers tend to save all the content that needs to be delivered in order in char Type array , Transmit network message and other information through pointer offset . It's very complicated to do this , Error prone , And once the control mode and communication protocol
There are changes , The program needs to be modified very carefully , Very error prone . Only one structure is needed at this time . At ordinary times, we require that the parameters of the function should not be more than 4 individual , If the function has more arguments than 4 It's very error prone to use ( Including the meaning and order of each parameter is easy to get wrong ), Efficiency will also be reduced ( And specific CPU of ,ARM Chip for more than 4 The processing of parameters is particular , For details, please refer to the relevant materials ). This is the time , The number of parameters can be compressed with the structure .

How big is the empty structure ?

The memory size occupied by a structure is the sum of the memory occupied by its members . It's easy to understand , But the following situation ?

struct student
{
    

}stu;

sizeof(stu) What is the value of alpha ?

unfortunately , No 0, It is 1. Why? ? Do you think , If we put struct student As a model , Can you make a mold without any volume ? Obviously not. . So does the compiler . The compiler believes that any data type has its size , Using it to define a variable can allocate a certain size of space . In that case , The compiler takes it for granted that any structure has a size , Even if this structure is empty . Nawan
A structure is really empty , Why is its size appropriate ? Suppose there is only one in the structure char The data member of type , Then its size is 1byte( Memory alignment is not considered here ). That is to say, the non empty structure type data needs to occupy at least one byte of space , The empty structure type data cannot occupy more space than the smallest non empty structure type data . That's the trouble , The size of the empty structure cannot be 0, It can't be greater than 1, What do I do ? Defined as 0.5 individual byte? But the smallest unit of memory address is 1 individual byte,0.5 individual byte How to deal with ? The best way to solve this problem is to compromise , The compiler takes it for granted that you construct a structural data type to package some data members , And the smallest data member needs 1 individual byte, The compiler reserves at least for each structure type data 1 individual byte Space . therefore , The size of the empty structure is positioned 1 individual byte.

原网站

版权声明
本文为[It's Beichen not too PI acridine]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061439469071.html