Calculation of structure memory in C language

structure memory

1. Ask questions

     The structure occupies a continuous memory space, and the size is determined by the type of the member variable. But it is not as simple as calculating the sum of the type sizes of all member variables .

Give an example first:

struct student {
	int age; //4个字节
	int telephone;  //4个字节
};
int main()
{
	struct student s;
	//      s的字节为8;
	printf("%d", sizeof(s));
	return 0;
}

As shown in the figure, the structure student contains 2 member variables of type int. After the code runs, it can be seen that the size of the structure is 8 bytes.

But is it really because of the sum of the sizes of the two member variables?

Let's have another example:

struct student {
	int age; //4个字节
    char name; //1个字节
	int telephone;  //4个字节
};
int main()
{
	struct student s;
	//      s的字节为12;
	printf("%d", sizeof(s));
	return 0;
}

The three member variables in the figure are  int, char, and int types, and the memory of the three member variables adds up to 9 bytes .

But why is the structure memory equal to 12?

 Obviously, the memory size of the structure  is not equal to  the sum of the memory sizes of all member variables

2. Calculate the memory size of the structure

When calculating the memory size of a structure, we must first learn a rule ----- alignment rule.

1. The first member is at the address whose offset is 0 from the structure variable .
2. Other member variables should be aligned to an address that is an integer multiple of a certain number (alignment number) .
     Alignment = Compiler's default alignment and the smaller value of the member size.
      (The default value in VS is 8)
3. The total size of the structure is an integer multiple of the maximum alignment number (each member variable has an alignment number) .
4. If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment number, and the
overall maximum alignment number (including the alignment number of the nested structure) Integer multiples.

The theory is out, now let's do the calculation in practice:

struct student {
	int age; //1个字节
    char name; //1个字节
	int telephone;  //4个字节
};
int main()
{
	struct student s;
	//      s的字节为12;
	printf("%d", sizeof(s));
	return 0;
}

For example, the above code, explained with the help of a diagram:

 

 Explanation ideas:

  1.  First of all, according to the rules, the first member must be at address 0 , and then the memory size of the member is 4 bytes, so it occupies 4 spaces, address 0~3 .
  2. The second member variable must start to follow the alignment rules . Since char is 1 byte, no matter what the address is, it is an integer multiple of 1 , and it also occupies a space, address 4 .
  3. Finally, the third member is of type int, at this time the address has reached the position of 4, and now it is necessary to align an integer multiple of 4 bytes of type int, it needs to start from 8 until it occupies 4 spaces, address 8~11 .
  4. End: The total size of the structure is an integer multiple of the maximum alignment (member variable memory) , and the maximum memory type of the structure is int 4 bytes, and the memory used at this time is 0~11, which is 12 . To meet the conditions.

 Note: If it is not satisfied, it needs to be expanded until it is satisfied.

 It's not easy to create, handsome guys and beautiful women passing by, move your little hands, like it, crabs

                                                                              If there is a problem with the article, please point it out and accept criticism with an open mind

Guess you like

Origin blog.csdn.net/m0_74097410/article/details/129046149