Custom types: structure (memory aligned), bit segment, enumeration, union

Structure

Structure declaration

Basic knowledge of
structure A structure is a collection of values, these values ​​are called member variables. Each member of the structure can be a variable of different types.

1. Statement of structure

struct tag //结构体类型名
{
    
    
    member_list;//结构体成员列表  
};

Note:
1. When declaring the structure type, the keyword struct must be used. The tag here can be omitted, but it is best not to omit it.
2. The structure type is only declared here, and it does not actually occupy memory. Only when structure variables are defined, memory is occupied.
3. Each member of the structure can be a variable of the same or different types, but there must be at least one member variable.

For example, describe a student:

struct Stu
{
    
    
	char name[20];
    int age;
    char sex[5];
    char id[20];
};//注意,分号不能丢

2. The self-reference of the structure
contains a type in the result body


struct Student
{
    
    
    int num;
    struct Student *stu;
};

No matter what type of pointer, it occupies 4 bytes on a 32-bit platform and 8 bytes on a 64-bit platform. In this way, the size of the structure variable can be determined.

3. Definition and initialization of structure variables

(1) Define variables while the structure is declared

struct Student
{
    
    
   char name[20];//姓名
   char sex;//性别
   int age;//年龄
   int num;//学号
}s={
    
    "zhangsan",'w',20,111};

(2) Define variables after declaring the structure

struct Student
{
    
    
    char name[20];//姓名
    char sex;//性别
    int age;//年龄
    int num;//学号
};
struct Student s={
    
    "zhangsan",'w',20,111};

(3) Access to
structure members There are two ways to access structure members: one is to access through structure variables, and the other is to access through structure pointers.

struct Student
{
    
    
    char name[20];//姓名
    char sex;//性别
    int age;//年龄
    int num;//学号
}ps;
struct Student s={
    
    "zhangsan",'w',20,111};
ps=&s;

(1) Access through structure variables:

printf("%s\n",s.name);

(2) Access through structure pointer:

printf("%s\n",ps->name);

4. Structure memory alignment (**)

Let's take a look at the following two examples:

struct A
{
    
    
    char a;//1
    int b;//3+4
    char c;//1
};
struct B
{
    
    
    char a;//1
    char c;//1
    int b;//2+4
};

The memory occupied by structure A is 12 bytes. The memory occupied by structure B is 8 bytes.

The composition of the members of the two structures is exactly the same, except for the different order, why the memory occupied is different? Here, it involves the memory alignment of the structure:

Structure memory alignment rules:

(1) The first member variable of the structure is always at the address where the offset is 0.
(2) The offset of the other structure member variables is at an integer multiple of the alignment. Alignment number: The minimum value of the compiler's default alignment number and the size of its own type. VS defaults to 8, and Linux defaults to 4.
(3) The maximum size of the structure must be an integral multiple of the maximum alignment.
(4) If the structure is nested, the alignment of the nested structure is its own maximum alignment.

Analysis: In structure A, the first member variable has an offset of 0 and occupies 1 byte, and the second member variable has an alignment of 4 and occupies 4 bytes, so the offset is 4 Place 4 bytes at the beginning, and the alignment number of the third member is 1, which occupies one byte, so place 1 byte from the offset of 8. At this time, a total of 9 bytes are occupied, but considering Rule (3), the maximum alignment number is 4, so a total of 12 bytes are occupied. Structure B can be considered in the same way.

Note: Structure memory alignment is to trade space for time

5. Modify the default alignment

#pragma pack(n);//n为对齐数

6. Structure transfer parameters The
structure transfer participates in the same array, and the structure pointer is transferred uniformly.

2. Position

The declaration of the bit segment is similar to the structure, with only two differences:

(1) The members of the bit segment must be integer or character type

(2) There is a colon and a number after the member name of the bit segment.
For example:

struct A
{
    
    
    int a:2;
    int b:10;
    int c:25;
};

What is the memory occupied by bit segment A?

First, a is of int type, 32 bits will be opened, and the variable a only occupies 2 bits, then, the variable b is followed by a occupies 10 bits, at this time there are 20 bits left, and the variable c 25 bits are needed, and the remaining 20 bits are not enough to accommodate, so an integer size will be created to store the 25 bits of variable c. Therefore, this bit segment occupies a total of 8 bytes.

3. Enumeration type

Enumeration types can list their members one by one, for example, 7 days of a week, from Monday to Sunday.
(1) Definition of enumeration type

enum Weekday
{
    
    
    Mon,
    Tues,
    Wed,
    Thur,
    Fri,
    Sat,
    Sun
};

note:

(1) The enumeration type declaration must use the keyword enum

(2) Except for the unsigned last enumeration member, the rest are separated by commas

(3) Enumeration members are all valued. By default, they start from 0, and initial values ​​can also be assigned. The subsequent members start from the initial value and increase by 1 sequentially. Therefore, enumeration members are also called enumeration constants

(2) The definition and assignment of
enumeration variables The definition of enumeration variables is similar to the structure, but only enum constants can be used for assignment.

4. Union (union) type

The characteristic of the union is that each member shares a memory space

(1) Joint type declaration

union U
{
    
    
    int i;
    char c;
};

Because the union member variables share a piece of memory, the memory occupied by the union type is 4 bytes.

(2) Definition and use of union variables

union U
{
    
    
    int i;
    char c;
}un;
    此时,&(un.c)和&(un.i)的结果是相同的。

Note: The addresses of all members in the union and the addresses of the union variables are the same in value.

Therefore, the type of union can be used to determine the size of the computer

(3) Calculation of the size of the union

Unions also need to consider memory alignment issues:

(1) The size of the union is at least the size of the largest member

(2) When the size of the largest member is not an integral multiple of the maximum alignment, the size of the union must be an integral multiple of the maximum alignment.

Guess you like

Origin blog.csdn.net/weixin_50843868/article/details/110007808