Explain structure memory alignment in detail

Table of contents

foreword

1. Calculation of memory size

1. Rules

2. Practice

2. Why should there be memory alignment?

1. Transplant cause

2. Performance reasons

3. Modify the default alignment number

Summarize


foreword

This article conducts an in-depth analysis of the calculation of the size of the structure. The size of the structure must comply with memory alignment, and in most cases, space will be wasted. But it has its value. After reading this article carefully, you will have a deep understanding of memory alignment and know its reasons.

1. Calculation of memory size

1. Rules

1. The first member is at the address with offset 0 from the structure variable

2. Starting from the second member, each member must be aligned to an integer multiple of an alignment number

This alignment is the smaller of its own member size and the default member alignment

Remark:

The default member alignment number in the vs environment is 8

3. When all the members are placed, the total size of the structure must be an integer multiple of the largest alignment number among all member alignments
 

4. If a structure is nested , the members of the nested structure must be aligned to an integer multiple of the maximum alignment of its own members.

The size of the entire structure must be an integer multiple of the maximum alignment, which includes the alignment of nested structures .

The above is the most common calculation, and I will help you understand it through a few questions

2. Practice

1) Calculate the size

struct S1
{
	char c1;
	int i;
	char c2;
};

Answer: 12

Detailed explanation:

Take the 32-bit system as an example.
The first char type c1 has a size of 1 and is placed at offset 0.

The second element is an int with a size of 4, and it should be placed on a certain alignment number, which is the smaller value 4 of itself 4 and the default alignment number 8. So i should be placed on a multiple of 4, the closest to i The position offset is 4, so three spaces were wasted before

The third element c2 should be placed on a multiple of its own size 1 and the smaller value 1 of the default alignment number 8. Since i occupies the position of 7, c2 should be placed at the position with an offset of 8. The total The size is 1+3+4+1=9

Finally, calculate the size of the total structure. The maximum number of members aligned is 4, but the above analysis is 9. If the multiple of 4 is not satisfied, 3 spaces will be wasted to 12.

The following is a diagram to help understand

2), calculate the size

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

Answer: 8

answer:

The first member c1 is at offset 0.

c2 is aligned to an integer multiple of the smaller value 1 of its own size 1 and the default alignment number 8, that is, at 1.

i is aligned to a multiple of 4, here is 4, 2 spaces were wasted before

The size is 1+1+2+4=8, which is an integer multiple of the maximum alignment number of members 4, so the size of the structure is 8

 3), calculate the size

struct S3
{
	double d;
	char c;
	int i;
};
printf("%d\n", sizeof(struct S3));

 Answer: 16

answer:

d is at offset 0, occupying 8 spaces in the future

c should be aligned to a multiple of 1, this question is 9

i needs to be aligned to a multiple of 4, this question needs to be aligned to 12, 3 spaces were wasted before, and 4 spaces will be occupied afterwards

The total size 8+1+3+4=16 is an integer multiple of the maximum alignment number of members 8, so the size of the structure is 16

4), structure nesting


struct S4
{
    char c1;
    struct S3 s3;
    double d;
};
printf("%d\n", sizeof(struct S4));

answer:

c1 is at offset 0, occupying a space later

s3 should be aligned to an integer multiple of an alignment number. Since s3 is a structure, the alignment number of the structure members is its own maximum alignment number, that is, the alignment number of d in s3, so it must be aligned to 8 places. Take up 16 spaces in the future

d should be aligned to an integer multiple of 8, that is, 24, and occupy 8 spaces later

The total size is 1+7 (wasted space)+16+8=32, which is an integer multiple of the maximum alignment number 8, so the size of the structure is 32

Answer: 32

2. Why should there be memory alignment?

1. Transplant cause

Not all platforms can access data in any space, so there must be a regulation to ensure effective transplantation

2. Performance reasons

On 32-bit, the hardware access (stack area) is 4 bytes at a time, and there is memory alignment, which can reduce the number of accesses when accessing the same data. Trade space for time .

So when designing the structure, try to put the members with small memory together

For example, topic 1 and topic 2, the member variables of the two are the same, but the order is different, and the space occupied is different

so:

The meaning of structure memory alignment is to exchange space for time. When designing, try to put small members together.

3. Modify the default alignment number

#pragma pack(Number to modify)

Modify the default alignment number, generally 2 to the nth power

Summarize

Structure memory alignment is a common test point. When aligning, space is generally wasted, but it is conducive to transplantation and helps to improve efficiency. Memory alignment Master the above four rules, you can do a job with ease.

I am Fanfan, thank you for reading!

Guess you like

Origin blog.csdn.net/m0_73299809/article/details/129651246