Explain custom types - structs

catalogue

Structure foundation

1. Structure declaration

2. Self reference of structure

3. Definition and initialization of structure variables

4. Structure member access (. And - > use)

Structure memory alignment

Modify the default number of alignments

Baidu interview questions

Structure foundation

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

1. Structure declaration

struct tag //Structure type name
{
 member-list; //Member variables (list)
}variable-list;//Variable list

Take a chestnut

struct stu
{
	char name[20];
	char sex[5];
	int age;
};

The above is the structure type. With the type, we can create the structure variable

struct stu
{
	char name[20];
	char sex[5];
	int age;
}s2,s3;//global variable
struct stu s5;//global variable
int main()
{
	struct stu s1;//local variable
	return 0;
}
When the structure is not completely declared, it can be declared
struct
{
	int a;
	char c;
	double d;
}sa;//Note that variables can only be defined here (anonymous structure variables)

2. Self reference of structure

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

Note that the structure cannot nest itself. To use it, please use the pointer (no dolls)

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

When renaming a structure with typedef, the modified name cannot be used internally, but the name before renaming should be used

3. Definition and initialization of structure variables

Serve chestnuts directly
struct stu
{
	char name[20];
	char sex[5];
	int age;
};
struct Data
{
	struct  stu s;
	char c;
	double b;
};
int main()
{
	struct Data sd = { {"lisi","nan",20},'a','3.14' };
}

4. Structure member access (. And - > use)

Structure memory alignment

Calculate the size of the structure
struct stu
{
	int a;
	char c;
	double d;
};
int main()
{
	printf("%d", sizeof(struct stu));//What is the size of the structure?
	return 0;
}

We can calculate that the size of int is 4, the size of char is 1, and the size of double is 8, that is, 4 + 1 + 8 = 13. Is the result right? Let's verify it

You can see that the result is 16. Why?

OK, let's talk about why it's different from what we expected. Before that, let's learn about a library function offsetof

offsetof is the offset of the calculated structure member from the starting position

Structure alignment rules

1. The first member is at the address offset from the structure variable by 0.
2. Other member variables should be aligned to the address of an integer multiple of a number (alignment number).
Number of alignments = the smaller value of the compiler default number of alignments and the size of the member.
The default number of alignments in VS is 8
Linux does not have a default alignment number, which is the size of the member itself
3. The total size of the structure is an integral multiple of the maximum alignment number.
4. If a structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number, and the integer of the structure is zero
The volume size is an integer multiple of the maximum number of alignments (including the number of alignments of nested structures).
Why is there memory alignment

 1. Platform reason (migration reason):

Not all hardware platforms can access any data at any address; Some hardware platforms can only take some special data at some addresses
Set the type of data, or throw a hardware exception.
2. Performance reasons :
data structure ( Especially stack ) It should be aligned on natural boundaries as much as possible.
The reason is: in order to access the misaligned memory, the processor needs to make two memory accesses; Aligned memory access requires only one access
Ask.

Memory alignment of structures is a method of trading space for time.
Let the members who occupy less space gather together as much as possible.

Modify the default number of alignments

You need to use #pragma pack() to modify the default alignment number #

Baidu interview questions

Write a macro to calculate the offset of a variable in the structure relative to the first address
#define OFFSETOF(s_name,n_name) (int)&(((s_name*)0)->n_name)
struct stu
{
	int a;
	char c;
	double d;
};
int main()
{
	printf("%d\n", OFFSETOF(struct stu, a));//0
	printf("%d\n", OFFSETOF(struct stu, c));//4
	printf("%d\n", OFFSETOF(struct stu, d));//8
    return 0;
}

Let's explain that we forcibly convert 0 to structure pointer - > member address is the offset, but if the type is structure type, we still need to forcibly convert it to integer

//#define OFFSETOF(s_name,n_name) (int)&(((s_name*)0)->n_name)
//                                (int)&(((struct s*)0)->a)

The above is the summary of the structure. If you think it's helpful, just praise it

 

Keywords: C

Added by 9902468 on Fri, 04 Mar 2022 01:18:07 +0200