Calculate the size of the structure (interview frequently asked)

foreword

Regarding the structure, I also introduced it before, but the concept is too general, ( study notes - the difference between the union and the structure and the coverage of the union ), and now I will sort out the detailed calculation method.

The assignment method of the structure

1. Initialization

Struct st{
    
    
    char a;
    int b;
   }x={
    
     ‘A’, 1 };

2. Assign values ​​by fields after defining variables

Struct st{
    
    
    char a;
    int b;
};
Struct st x;
x.a = ‘A’;
x.b = 1;

3. Assignment of structure variables

Struct st {
    
    
    char a;
    int b;
}; 
Struct st x,y;
x.a= ‘A’;
x.b=1;
y=x;

Structural field

Bit field type: char, short, int can be carried (signed or unsigned)

Bit field definition:

struct st{
    
    
	unsigned char a:7;  //字段a占用一个字节的 7bit(位)
	unsigned char b:2;  //字段b占用一个字节的 2ibt(位)
	unsigned char c:7;         
}s1;

Advantages of bit fields:

① It does not require complete bytes, saves storage space, and is easy to process;

② It is convenient to use the bit field to decompose a variable by bit field;

But it is not conducive to the transplantation of the program! !

Calculate the size of a structure sizeof(struct s1)

1. The concept of structure offset:

The offset in the structure refers to the distance between the actual address of a member and the first address of the structure.

2. Calculation method of structure size:

The structure will involve byte alignment, (the purpose is to allow the computer to read quickly and exchange speed with space), that is, the size of the last member + the offset of the last member + the number of padding sections at the end .

3. Offset rules within the structure

1. Make up the size of the above type bytes to become a multiple of the following type bytes.

2. Each type byte of the structure variable can be divisible by the largest type byte.

3. The total size of the structure is an integer multiple of the largest type.

4. The largest type is the single largest type, not the total size of the calling structure.

struct function
{
    
    
	int a;//4
	char b;//1
};//总大小8
 
struct test
{
    
    
	float x;//4
	struct function func1;//调用结构8
};//总大小12  单个最大类型为4

insert image description here

For practice, look at this example.

Struct s1{
    
          成员的大小       成员的偏移量

     int   a;           4                0          
     char  b;           1                4
     int   c;           4                5+3(填充字节)
     long  d;           8                12+4(填充字节)
     char  e;           1                24
}

Sizeof(s1)=24+1+7 (filling bytes)

4. Structure member array size

Struct st {
    
    
    int a;
	int b;
	char c[0];
}st_t; 

sizeof(st_t) = 8

Struct book {
    
    
	 char    name[5];
	 float   price;
}book[2];

Sizeof( book ) = 12;

Sizeof (book[2]) = 24;

insert image description here

Step 1: The element whose structure member accounts for the largest number of bytes is sizeof ( float ) = 4; use 4 to allocate other members;

Step 2: If the array exceeds four bytes, the newline will continue, and if the remaining byte is not enough for float data, then the newline will be used.


Summarize

1. The offset of a structure member must be an integer multiple of the member size (0 is considered an integer multiple of any number) 2. The structure
size must be an integer multiple of the size of all members (except arrays and structures).

Guess you like

Origin blog.csdn.net/qq_44333320/article/details/125701490