Custom type (structure, how to calculate the storage size of the structure)

Table of contents

What is a structure: 

How to declare a structure:

 The definition of the structure:

How to calculate the size of a struct type?

Structure memory alignment


When we want to define a student type in C language, we will find that if we define it with basic data types such as char and int, it will not be possible, because a student type will have a student number, name... and at this time we will use To the structure in the custom type in C language.

What is a structure: 

Structure: is a collection of values ​​called member variables. Each member of the structure can be a variable of a different type.

A structure definition consists of the keyword struct and a structure name, and the structure name can be defined as required.

How to declare a structure:

 For example, if you want to define a student type, you can write:

struct Stu
{
    int age;
    char name[10];
};

The struct is a keyword defining the structure type (must be written);

Stu is the name of the current structure (you can leave it blank);

The age and name in curly braces are member variables .


 The definition of the structure:

 One is to define the same as ordinary variables, such as variable a:

struct Stu
{
	int age;
	char name[10];
};

int main()
{
	struct Stu a = {18, "zhangsan"};
	return 0;
}

Another way is to define such as s1 when declaring the structure ( the life cycle of the structure variable defined by this method is the same as the type of the structure ) If you do not write the name of the structure, you can only define it in this way A variable of this type is s2:

struct Stu
{
	int age;
	char name[10];
}s1;

struct
{
	int age;
	char name[10];
}s2;

How to calculate the size of a struct type?

Let's use the above structure as an example. The following picture should be the easiest one for us to think of, and then we will get that the size of this type is 4+10=14 bytes.

 However, after we used sizeof to calculate, we found that this is not the case. The real result is 16;

Then we need to use offsetof in order to get the relative storage position of the structure member variable in memory ( it can calculate the offset of the structure member compared to the start position of the structure, it is not a function but a macro )

Example 1:

Example 2:

 From these two examples, it can be seen that the structure member variables are not stored continuously in memory.

And this is called:

Structure memory alignment

Alignment rules:

  1. The first member is at offset 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; there is no default alignment number in Linux, and the alignment number is the size of the member itself
  3. The total size of the structure is an integer multiple of the maximum alignment (each member variable has an alignment).
  4. If a structure is nested, the nested structure is aligned to an integer multiple of its own maximum alignment, and the overall size of the structure is an integer multiple of all maximum alignments (including the alignment of the nested structure).

 Now we can explain the above two examples according to the rules

 Example 1:

 

 The offset of age is 0, and the type of name is char, so it is stored next to it, but the size of the entire structure type is 14 not a multiple of 4 (because the size of int is 4 bytes), so two bytes are wasted to become 16 bytes. According to the alignment rules, the size of Example 2 can also be calculated

Example 2:

 

 

Guess you like

Origin blog.csdn.net/2302_76339343/article/details/131682053