Memory alignment of structures, unions and their simple applications

Table of Contents

1. Memory alignment of structures

1. Memory alignment rules

2. Give examples

3. Why does memory alignment exist?

2. Consortium

1. A brief introduction to the consortium

2. Give examples

3. Calculation of consortium size

4. Simple application of consortium


1. Memory alignment of structures

1. Memory alignment rules

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

2. Other variables must be aligned to addresses that are integer multiples of the alignment number (alignment number = the smaller value of the compiler’s default alignment number and the size of the member, where the default alignment number in vs is 8, and there is no default alignment number in Linux. The alignment number The size is the size of the member itself);

3. The total size of the structure is an integer multiple of the maximum alignment number;

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

2. Give examples

From the offsets of different variables, we can conclude that the space opened by variables is as follows:

According to the first rule, c1 is opened at the offset of 0, the type of c2 is char, the size is 1 byte less than 8 bytes, the alignment number is 1, and the alignment is at an address that is an integer multiple of it, so the alignment is at the offset At the offset of 1, i takes the same alignment number as 4. Because it is at an address that is an integer multiple of the alignment number, addresses 2 and 3 are wasted. For it at an offset of 4, the maximum alignment number is 4. , the structure size is an integer multiple of 4, so the structure size is 8 bytes (this type of explanation will not be repeated later).

The structure size is 8 bytes, and 2 bytes are wasted after c2.

The structure size is 12 bytes, with 3 in the middle and 3 bytes wasted at the end.

The structure size is 12 bytes, with 3 in the middle and 3 bytes wasted at the end.

It is not difficult to see that in order to save space we have to gather members who occupy less space together.

In addition, the default alignment number can be set as follows:

Add #pragma pack() before the created structure type, and the default alignment number to be modified is in parentheses.

3. Why does memory alignment exist?

1. Platform reasons (transplantation reasons):

Not all hardware platforms can access any data at any address. Some hardware platforms can only read certain types of data at certain addresses, otherwise a hardware exception will be thrown;

2. Performance reasons:

Data structures (especially stacks) should be aligned on natural boundaries whenever possible. The reason is that in order to access unaligned memory, the operating system needs to do two accesses, while aligned memory only needs one access.

For example (under 32-bit platform) access i:

If not aligned:

First visit:

Second visit:

If aligned:

i only needs to be accessed once;

Summary: Structure memory is a way of trading space for time. When designing, we must meet both alignment and space saving requirements. We must gather members that take up little space together as much as possible.

2. Consortium

?1. A brief introduction to the consortium

Union is also a self-defined type. The variables defined by this type also include a series of members. Similar to the structure, the members of the variables defined by the union can also be of different types. The characteristic is that they share a space. .

2. Give examples

The created union variable can be obtained from the running results. The addresses of the union variable members are consistent. Verify that the members mentioned above share the same space.

3. Calculation of the size of the consortium

1. The size of the union is at least the size of the largest member;

2. When the union size is not an integer multiple of the maximum alignment number, it must be aligned to an integer multiple of the maximum alignment number.

The char type array c can be regarded as 6 char type variables, each alignment number is 1, and the alignment number of i is 4. Since it needs to be aligned to an integer multiple of the maximum alignment number, which is an integer multiple of 4, the union The size is 8, that is, i and c share 4 bytes and then use 2 bytes. Because they are aligned to 8, they apply for 2 bytes of space again.

4. Simple application of consortium

Verify whether the current machine has big-endian storage or little-endian storage?

Method 1 (using pointers):

int check_sys()
{
int i = 1;
return *(char*) &i;
}
int main()
{
\t
if (check_sys())
printf("Little endian storage\
");
else
printf("Big endian storage\
");
return 0;
}

Use different types of pointer access steps to determine;

Method 2 (Utilizing Consortium):

int main()
{
union Un
{
char c;
int i;
};
union Un u;
u.i = 1;
printf("%d\
", u.c);
return 0;
}

Use the union shared space to perform partial data access to i by accessing c.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge C Skill Tree Structure Structure and Function 189385 people are learning the system