Use of custom data types struct, enum and union

1, Structure: a set of data representing a specific meaning is always stored
Features: it encapsulates data (function encapsulates function)
Benefits: 1 Improve code readability
2. Improve data usability
3. Improve code maintainability
Structure definition format:

struct +Structure name
{
Element 1;
.....
}

Structure definition structure variables:
struct + structure name + variable name (in c language),
Structure name + variable name (under c + +).
Memory alignment:
Meaning: changing space for time can avoid the secondary reading of memory and reduce the number of cpu memory accesses.
method:
1. Starting from the first attribute, the memory starts from 0, and the second attribute should be placed on an integer multiple of min (the size of the attribute, alignment modulus).
2. After all attribute calculations are completed, perform secondary offset as a whole. When all attribute offsets are completed, take the integer multiple of min (the largest element of the structure, alignment modulus).
Note:
1. The starting memory of a variable must be an integer multiple of it, such as char a; It occupies one byte, but followed by int type data, so it can only start from the integer multiple of 4 at this time, resulting in the fact that the data occupying one byte occupies 4 bytes
2. The number of bytes occupied is not invariable. You can modify the memory size by modifying the alignment modulus by default.

eg:
struct student
{             First offset       Second offset (start memory must be min(Current type size,alignment modulus )Integer multiple)
    char a;//   0-3 (because the address of b must start from 4)
    int b;//    1-4           4-7
    double c;// 5-12          8-15
}
Therefore, it is 16 bytes after alignment.
When the element order is swapped, its size may change
struct student
{  The value is given directly here, and the analysis steps are not written
   int b;              //0-7
   double c;           //8-15
   char a;             //16 now takes up 17 bytes, so the result is 24 bytes (memory alignment)
}
//Therefore, when defining a structure, elements should be defined from small to large without special requirements

The memory occupied by a structure itself is not invariable. You can modify the memory occupied by modifying the alignment module.
Format: #pragma pack(num)//num is given by user bytes
#pragma pack(show) / / you can view the alignment modulus of this machine. The default is 8

eg:
#pragma pack(1)
struct student
{
  char a;//0
  int b;//1-4
  double c;//5-12
}//When the alignment modulus is 1, 13 bytes are occupied at this time.

Differences between c language structure and c + + structure:
1. Under c + +, the struct keyword can be omitted when defining structure variables.
2. Under c + +, member functions can be defined in the structure
3. Under c + +, a structure can be inherited by another structure. It is public inheritance by default.

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;


//In c + +, the struct can contain member functions
struct student
{

	int age;
	char name[20];

	void show();//It can be declared in the structure, defined outside the class, or directly placed inside the structure
};
//Structures in c + + can be inherited. By default, they are public inheritance
struct teacher :student
{
	double score;
};
void student::show()//There is an implicit this pointer to this part of the space
{
	/*printf("Name% s\t", name);
	printf("Age% d\n", age);*/
	printf("full name%s\t", this->name);
	printf("Age%d\n", this->age);
}
void test01()
{
	cout << sizeof(student) << endl;//Conclusion: when there are other elements, the function does not occupy any space, but when there is only one function in a structure, it will occupy 1 byte
	student t1 = { 10,"Zhang San" };//Structure variables can be defined without struct keyword
	t1.show();
}
void test02()
{
	cout << sizeof(teacher) << endl;//At this time, the value is 32 = 24 + 8 It is similar to calculating the stdent element
	teacher t;//t variable can call any variable of the inherited structure, including its member functions
	t.age = 10;
	strcpy(t.name, "Zhang San");
	t.score = 90.0;
	t.show();
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

Distinguish between c and c + + when nesting structures

struct teacher
{
	char c;//4
	struct tt
	{
		int a;//4
		char d;//4
	};
	int age;//4
};//The sizeof() value is 16 in c language and 8 in c + +. The compiler will not nest tt
 Allocate memory.
//Note: when the nested structure is an unnamed structure, the result of c/c++sizeof is the same.

2, Use of offsetof()
Purpose: find the number of bytes passed by the member address in the structure relative to the first value of the structure.

struct student
{

	int b;              //0-7
	double c;           //8-15
	char a;
};
int main(void)
{
	struct student t1 = { 10,20.0,'a' };
	int len = offsetof(struct student, a);
	
	printf("%c\n", *(char*)((char*)(&t1) + len));//The second (char *) (& T1) + len) from left to right is to shift it byte by byte,
	//The first char * found is the type conversion, and its value is obtained after dereference
	//printf("%d\n", sizeof(struct student));
	system("pause");
	return EXIT_SUCCESS;
}

3, Bit field (concept of bit segment)

Reasons for introducing bit fields:
When some information is stored, it does not need to occupy a complete byte, but only several or one binary bit. For example, when storing a switching value, there are only two states: 0 and 1, and one binary can be used. In order to save storage space and make processing simple, C language provides a data structure called "bit field" or "bit segment". The so-called "bit field" is to divide the binary bit in a byte into several different regions and explain the number of bits in each region. Each domain has a domain name, which allows you to operate by domain name in the program. In this way, several different objects can be represented by one byte binary field.

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
//The reason for introducing bit field: some information does not need to occupy a complete byte, but only several or one binary bit.
//For example, when storing a switching value, there are only two states: 0 and 1, and one binary can be used. In order to save storage space and make processing simple
//, C language also provides a data structure called "bit field" or "bit segment". The so-called "bit field" is to divide the binary in a byte into several different areas,
//And indicate the number of digits in each area. Each domain has a domain name, which allows you to operate by domain name in the program. In this way, several different objects can be represented by one byte binary field.
struct student
{
	char a : 2;
	char b : 4;
};
//1. A bit field must be stored in the same byte and cannot span two bytes
struct t
{
	int a : 5;
	int b : 5;
};
//This part of the name space is not available
struct t1
{
	int a : 30;
	int : 2;
	int b : 2;
};
//3. The length of the bit field cannot be greater than the length of one byte of the data type eg: int a: 33// err
//4. All bit fields cannot be stored across types, and the types must be consistent
struct t2
{
	/*int a : 2;
	char b : 2;
	int c : 20;*///When different types of bit fields appear, the system defaults that the bit field has ended, and the sizeof evaluation is 12
	int a : 2;
	int c : 20;
	char b : 2;
	
};
void test01()
{
	printf("%d\n", sizeof(struct student));
	printf("%d\n", sizeof(struct t));
	printf("%d\n", sizeof(struct t1));
	printf("%d\n", sizeof(struct t2));
}

int main(void)
{
	test01();
	system("pause");
	return EXIT_SUCCESS;
}

3, Enum enum

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
enum a {
	mon,
	tus,
	wed,
	thur,
	fri,
	satur,
	sun,
    
};
void test01()
{
	printf("%d\n", sizeof(enum a));//1. No matter how many elements there are in the enumeration, their size is 4 or unchanged.
	//Enumeration replaces the name with the corresponding value at compile time. We can understand enumeration as a macro in the compilation stage.
	//2. Enumeration by default, elements start from 0 and increase in order.
	enum a t1=0;
	for (size_t i = 0; i < 7; i++)
	{
		printf("%d\t", t1++);
		
	}
}
//enum is used with switch statement
void test02()
{
	enum a t1 = mon;
	
	while(1)
	{int n=scanf("%d", &t1);
	if (n == 0)
	{
		printf("Input type does not match, jump out of loop\n");
		return;
	}
		switch (t1)
		{
		case mon: {printf("1\n"); break; }
		case tus: {printf("2\n"); break; }
		case wed: {printf("3\n"); break; }
		case thur: {printf("4\n"); break; }
		case fri: {printf("5\n"); break; }
		case satur: {printf("6\n"); break; }
		case sun: {printf("7\n"); break; }
		default: {printf("error number\n"); continue; }
		}
		
	}

}
int main(void)
{
	//test01();
	test02();
	system("pause");
	return EXIT_SUCCESS;
}

4, Use of community union
Because it can't be written, the corresponding concepts will be presented in the code

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <Windows.h>
//Common body:
//Features: the size of the common body is always the value of the largest element in the common body
//      Only one element of the common body is valid for a period of time, and one element fails
//      The problem of memory alignment must also be considered
union a
{
	int a;
	char c;
};
//Differences between common body and struct:
//All members of the union refer to the same location in memory. When you want to store different things in the same place at different times, you can use union.
//All variables in the structure are "coexisting" - the advantage is that "tolerance leads to greatness" and comprehensiveness; The disadvantage is that the allocation of struct memory space is extensive. It is fully allocated whether it is used or not.
//In a union, variables are "mutually exclusive" - the disadvantage is that they are not "inclusive"; But the advantage is that the use of memory is more precise and flexible, and also saves memory space.
//The structure can call all variables at one time, but the common body cannot. Only one can be valid and one can be invalid.
void test01()
{
	union a s1;
	s1.a = 10000;//0010 0111 0001 0000
	printf("%d\n", s1.c);//At this time, c is 16 0001 0000
}
//Using the common body to judge the size end
void test02()
{
	union a s1;
	s1.a = 1;
	if (s1.a & 1 == 1)
	{
		printf("Small end\n");
	}
	else
	{
		printf("Big end\n");
	}
}
//Use the storage mode in the memory to judge the size end
void test03()
{
	int a = 0x01;
	if (a && 1)
	{
		printf("xiao\n");
	}
	else
	{
		printf("da\n");
	}
}
int main(void)
{
	//test01();
	//test02();
	//test03();
	typedef struct {
		int a;
		char b;
		short c;
		short d;
	}AA_t;
	printf("%d\n",sizeof(AA_t));
	system("pause");
	return EXIT_SUCCESS;
}


Keywords: C C++

Added by expertis on Sat, 19 Feb 2022 23:22:10 +0200