[Structure memory allocation problem]

Rule 1: How many bytes to open up memory

When allocating memory to a structure variable, it will go to the structure variable to find the members of the basic type. Whichever member of the basic type occupies more bytes, the memory is opened up in units of its size, and there is an exception of the double type in gcc.

  1. There is only char data in the member, and the memory is allocated in units of 1 byte.
  2. The short type data appears in the member, and there is no basic type data with a larger number of bytes.
    Allocate memory in units of 2 bytes
  3. When there are int, float and no larger byte basic type data, the memory is allocated in units of 4 bytes.
  4. Data of type double appears
    • Case 1: In vc, memory is allocated in units of 8 bytes.
    • Case 2: In gcc, memory is allocated in units of 4 bytes.
      Regardless of the environment, the double type variable occupies 8 bytes.
  5. If an array appears in the structure, the array can be regarded as a collection of multiple variables.
    If there is a pointer, if there is no type that occupies a larger number of bytes, the memory is allocated in units of 4 bytes.
  6. When storing structure members in memory, store them in the order of the defined structure members.

Rule 2: Byte Alignment

  1. char: 1-byte alignment, that is, to store char-type variables, and the number of the memory unit is a multiple of 1.
  2. short: 2-byte alignment, that is, to store short int variables, and the number of the starting memory unit is a multiple of 2.
  3. int: 4-byte alignment, that is, to store int-type variables, and the number of the starting memory unit is a multiple of 4.
  4. long: On a 32-bit platform, 4-byte alignment, that is, to store long int variables, the number of the starting memory unit is a multiple of 4.
  5. float: 4-byte alignment, that is, to store float-type variables, and the number of the starting memory unit is a multiple of 4.
  6. double:
    • In the vc environment: 8-byte alignment, that is, the starting address for storing double variables must be a multiple of 8, and double variables occupy 8 bytes
    • Under the gcc environment: 4-byte alignment, that is, the starting address for storing double variables must be a multiple of 4, and double variables occupy 8 bytes.

Note:
When an array appears in a structure member, it can be regarded as multiple variables.
When allocating memory, open up space according to the position order of members in the structure from top to bottom.

struct stu{
    
    
	 int num;
	 int age;
}lucy; //8字节, 4字节的倍数
struct stu{
    
    
	char sex;
	int age;
}lucy; //8字节,4字节的倍数
struct stu{
    
    
	char a;
	short int b;
	int c;
}temp; //8字节,4字节的倍数,char 1 字节 + short 2 字节 + int 4 字节 < 8 字节
struct stu{
    
    
	char a;
	int c;
	short int b;
}temp; //12字节,4字节的倍数
struct stu{
    
    
	char buf[10]; // 1*10 个字节
	int a; // 4 个字节
}temp; //16字节
struct stu{
    
    
	char a; // 1 个字节
	double b; // 8 个字节
}; //12字节, 4字节的倍数

Why should there be byte alignment?
Use space to exchange time to improve the efficiency of CPU reading data.

Guess you like

Origin blog.csdn.net/shuting7/article/details/130685222