Structure enumeration union, size calculation

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right


1. Structure

The declaration of a structure; a structure is a collection of values, these values ​​are called member variables. Each member of the structure can be a different type of variable

struct Stu
{
    
    
 char name[20];//名字
 int age;//年龄
 char sex[5];//性别
 char id[20];//学号
}//分号不能丢

Structure self-reference

typedef struct Node
{
    
    
 int data;
 struct Node* next;
}Node

Calculate the size of the structure

(Alignment is an effective way to use space for time in a
structure ) First of all, you must master the alignment rules of the structure:

  1. The first member is at an address with an offset of 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).
  3. The total size of the structure is an integer multiple of the maximum alignment (each member variable has an alignment).
  4. If the structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment, and the overall size of the structure is an integer multiple of all maximum alignments (including the alignment of nested structures).
  5. Alignment number = the smaller value of the compiler default alignment number and the member size
  6. The default value in VS is 8
struct S2
{
    
    
	char c1; //1
	char c2; //1 + 2
	int i;   //4
};

//16
struct S3
{
    
    
	double d; //8
	char c;   //1 + 3
	int i;    //4
};

struct S4
{
    
    
	char c1;       //1 + 7
	struct S3 s3;  //16
	double d;      //8
};

struct S1
{
    
    
char c1; //1 + 3
int i;   //4
char c2; //1 + 3
};

There are also nested patterns

typedef struct Test
{
    
    
	short a;      //2 + 6
	struct
	{
    
    
		int b;    //4 + 4
		double c; //8
		char d;   //1 + 7
	};
	int e;        //4 + 4
}Test;//40

typedef struct Test
{
    
    
	short a;      //2 + 6
	struct
	{
    
    
		int b;        //4 + 4
		double c[10]; //80
		char d;       //1 + 7
	};
	int e;        //4 + 4
}Test;//112

#pragma pack(2)//可以自指定大小,但不一定是有效值
typedef struct Test
{
    
    
	short a;      //2
	struct
	{
    
    
		int b;        //4
		double c;     //8
		char d;       //1 + 1
	};
	int e;        //4
}Test;//20


Bit segment

What is a
bit segment The declaration and structure of a bit segment are similar, with two differences:
1. The members of a bit segment must be int, unsigned int or signed int.
2. There is a colon and a number after the member name of the bit segment.

typedef struct Test
{
    
    
	char a : 1; // 0 1  开关值  位域
	char b : 6; // 0 1
	char c : 3; // 0 1=
}Test;//( and b) c  2

typedef struct Test
{
    
    
	char a : 1;
	int  b : 1;
}Test;//8 不同的不能通用,就还是要补齐

The stored data of the bit segment will only be stored in the space created by itself, even if the number of digits is not stored, it will be truncated immediately.
Memory allocation

  1. The members of the bit segment can be int unsigned int signed int or char (belonging to the integer family) type
  2. The space of the bit segment is opened up in 4 bytes (int) or 1 byte (char) as needed.
  3. Bit segments involve many uncertain factors. Bit segments are not cross-platform. Programs that focus on portability should avoid using bit segments.
    Cross-platform issues
  4. It is uncertain whether the int bit field is treated as a signed number or an unsigned number.
  5. The number of largest bits in the bit segment cannot be determined. (The 16-bit machine is up to 16, and the 32-bit machine is up to 32, written as 27, which will cause problems on 16-bit machines.
  6. The members in the bit segment are allocated from left to right in memory, or the criteria for allocation from right to left have not been defined.
  7. When a structure contains two bit segments, and the second bit segment member is relatively large and cannot fit in the remaining bits of the first bit segment, it is uncertain whether to discard the remaining bits or use them.

Two, enumeration

Enumeration, as the name implies, enumerates one by one.
List the possible values ​​one by one.

typedef enum TYPE
{
    
    
	ADD,//0
	SUM,//1
	MUL,
	DIV,
	MOD,
	SQU
}TYPE;
void main()
{
    
    
	printf("%d %d %d %d\n", ADD, SUM, MUL, DIV);//0 1 2 3
	printf("size = %d\n", sizeof(enum TYPE));//4
}

When a number is given, the latter will add one to the previous number.

Three, joint

Union is also a special custom type. Variables defined by this type also contain a series of members. The characteristic is that these members share the same space (so union is also called union).

Joint characteristics

The members of the union share the same memory space. The size of such a union variable is at least the size of the largest member (because the union must at least be able to store the largest member).

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 integral multiple of the maximum alignment, it must be aligned to an integral multiple of the maximum alignment.

union Test
{
    
    
	int a;
	double b;
	char c;
};

void main()
{
    
    
	printf("size = %d\n", sizeof(union Test));//8
	union Test t;
	t.b = 12.34;
	printf("%f\n", t.b);//12.340000
	t.a = 100;
	printf("%d\n", t.a);
	t.c = 'A';

	printf("%f\n", t.b);//12.3339996
	printf("%d\n", t.a);//65
}

Because the memory size is shared, the data of the previous one will change after the next assignment.

Guess you like

Origin blog.csdn.net/weixin_45070922/article/details/110189468