C basic syntax (custom type)

Table of contents

Structure:

declaration of the structure

Calculation of structure size

Reasons for memory alignment

bit segment:

bit field declaration

Allocation of bit segment memory space

The role of bit segment

enumerate

enum definition

Advantages of Enums

Union (community)

joint definition

Calculation of joint size


Structure:

declaration of the structure

The members of the structure can be variables of different types. When declaring the structure, it does not actually create a structure, but is equivalent to a design drawing, which is used to illustrate how the structure is designed. There are which members etc.

struct stu
{
 char name[20];
 int age;
 float sroce;
};

//这样就完成了结构体的声明,stu是这个结构体的标签,代表着这个结构体的设计类型



//结构体的创建
struct stu
{
 char name[20];
 int age;
 float sroce;
}s1, s2;

int main()
{
struct stu s3, s4;
return 0;
}

//s1,s2,s3,s4都是我们创建的结构体变量,其中s1和s2是全局型变量,在同一源文件内,任何函数内部都可以直接调用,s3和s4是局部型结构体变量,只能由main函数内部直接调用,其他函数要想用需要传参过去。


//如果嫌struct stu 太长,可以用typedef 将struct stu 换个名字

typedef struct stu
{
 char name[20];
 int age;
 float sroce;
} stu;

//利用 typedef 将struct stu 的名字换成stu。  以后stu就相当于struct stu

There is also a declaration of a structure called an anonymous structure. An anonymous structure has no tag name, so this structure can only be created and used once. The usage scenario of an anonymous structure is suitable for only using this type of structure once. See An example below.

//匿名结构体类型
struct
{
int a;
char b;
float c;
}x;

struct
{
int a;
char b;
float c;
}a[20], *p


//上面两个结构体都省略了结构体标签,表明它们都是匿名结构体.
//但是 p = &x;是错误的,尽管两个结构体的内部设计是一样的,但因为没有标签,编译器会识别为这是两个不同类型的结构体,这也是为什么匿名结构体只能使用一次。

Calculation of structure size

To calculate the size of the structure, you need to understand the rules of the memory alignment of the structure

1. The first structure member is at offset 0 of the structure start address

2. Other structure member variables should be aligned to an address that is an integer multiple of a certain number (alignment number)

"Alignment" = the compiler's default alignment and the smaller value of the member's own size (vs the default alignment of 8)

3. The total size of the structure is an integer multiple of the maximum alignment (alignment of structure members)

4. If the structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number, and the overall size of the structure is the integer of all maximum alignment numbers (including the alignment number of the nested structure) times

Here are some examples

struct s
{
	char i;
	int a;
	char b;
}m;

//计算m所占空间的大小

 Therefore, the size of the structure is 12 bytes

struct S2
{
char c1;
char c2;
int i;
}m;

//计算结构体m的大小

So the size of the structure is 8 bytes

Reasons for memory alignment

From the calculation of the size of the above structure according to the principle of memory alignment, it can be seen that some memory is not used, resulting in waste of memory, so why not store the members of the structure in sequence, but to implement a memory alignment rule?

1. Platform porting

Not all hardware can access any data at any address. Some hardware platforms can only access certain data types at certain addresses. If the structure is not stored according to certain rules, some hardware will The data in its memory cannot be accessed, and the default alignment number can be modified through the preprocessing instruction #pragma(). As mentioned earlier, the default alignment number of VS is 8, which is modifiable. Whether it is modified or not depends on the purpose of the program.

2. Performance improvement

On a 32-bit machine, memory alignment is more conducive to reading data. If memory alignment is not performed, it may be necessary to read more times when reading a certain data in the structure. Memory alignment has the effect of exchanging space for time.

bit segment:

bit field declaration

Members of bit fields must be int, unsigned int, signed int or char

A colon and a number follow the member name of a bit field

struct m
{
  int _a:2;
  int _b:5;
  int _c:10;
  int _d:30;
};

//m就是一个位段类型

Allocation of bit segment memory space

The bit field space is opened up by 4 bytes (int) or 1 byte (char)

Because there are many uncertain factors in the bit segment, the bit segment is not cross-platform, and the program that needs to be transplanted should use as little bit segment as possible. Here are some examples to illustrate the memory allocation of the bit segment

struct S
{
  char a:3;
  char b:4;
  char c:5;
  char d:4;
}s;

//求位段s各成员的内部分配

 First open up a space of char type, the bit field member variable a occupies 3 bits, and then the bit field member variable b occupies 4 bits. At this time, there is only one bit left in the first char space, which obviously cannot be put down. The bit field member variable c that occupies 5 bits. Therefore, it is necessary to open up another char type space for the bit segment member variable c, but at this time there are only 3 bits left in the second char type space, which cannot fit the bit segment that occupies 4 bit space Member variable d, so it is necessary to open up a third char type space to store the bit segment member variable d, and finally the bit segment type occupies 3 bytes of memory.

The role of bit segment

The bit segment seems to divide a space of char or int into several parts for use, so under what circumstances will the bit segment be used, here is a common usage.

 Looking at the datagram in the above figure, if the data in each frame above is allocated an int type to store, it is conceivable that such a space consumption is very huge, and the data transmission will be very slow, while the 4-bit version, 4 The length of the bit header, 16-bit identifier, etc. are stored in an int type, so that the bit segment is used, which greatly saves space consumption.

enumerate

enum definition

Enumeration is to enumerate the possible values ​​one by one. The members inside the enumeration are all possible values, also called enumeration constants. If the enumeration constant is not assigned an initial value, the default value of the first member is 0, and the value of the second member is 1, increasing in turn. Of course, you can also assign an initial value to each member.

enum Day//星期
{
  Mon,
  Tues,
  Wed,
  Thur,
  Fri,
  Sat,
  Sun
};

//这里Mon的值为0,Tues的值为1,依次递增,创建一个枚举变量,该变量的取值只能为枚举成员之一。
//enum Day s = Mon;   s的取值只能为枚举内的某个成员



enum direction
{
  right = 77;
  left = 75;
  up = 72;
  down = 80;
}

//当然我们可以在定义枚举时,给每个成员赋初始值,上面方向的枚举就是游戏设计中常用到的取值,每个方向的取值通过ASCII码对应着键盘上的某个字母。

Advantages of Enums

1. Increase the readability and maintainability of the code
2. Compared with the identifier defined by #define, the enumeration has type checking, which is more rigorous.
3. Prevent naming pollution (encapsulation)
4. Easy to debug
5. Easy to use, multiple constants can be defined at a time
 

Union (community)

joint definition

Union is also a special custom type.
The variable defined by this type also contains a series of members, which are characterized by the fact that these members share the same space (so union is also called union)

Because they share the same space, the size of the union is at least the size of the largest member, so that the largest member can fit

union Un
{
int i;
char c;
};
union Un un;

//描述un联合体的空间分布

 Calculation of joint size

The size of the union is at least the size of the largest member.
When the maximum member size is not an integer multiple of the maximum alignment number, it must be aligned to an integer multiple of the maximum alignment number

typedef union Un1
{
	char c[5];
	int i;
}un1;

typedef union Un2
{
	short c[7];
	int i;
}un2;
int main()
{
	printf("%d  %d  \n",sizeof(un1), sizeof(un2) );
	return 0;
}

//计算结果为 8 16

Reason: For un1, char c[5] is the largest member, occupying 5 bytes of space continuously, int i and char c[5] share the space of 5 bytes, but the final size of the joint To follow the alignment rules, each member of char c[5] is char, so its alignment number is 1, and the alignment number of int i is 4, then the maximum alignment number within the member is 4, and the final size is the maximum alignment number Integer multiple, the current size is 5, not an integer multiple of 4. Therefore, 3 more bytes must be added to achieve alignment, and the final size is 8. The same is true for un2.

Guess you like

Origin blog.csdn.net/m0_61350245/article/details/125917816
Recommended