Data Alignment of struct in windows

Source: Internet
Author: User
Preface

For example, some platforms start from the even address each time they read data. If an int type (assuming a 32-bit System) is stored at the beginning of the even address, a read cycle can be read, if the data is stored at the beginning of the odd address, it may take two read cycles and splice the high and low bytes of the two read results to obtain the int data.Data Alignment)This requirement can improve the performance of the memory system and reduce the number of addressing times, at the cost of wasting some space. In other words, we use a relatively cheap space for time.

Data Alignment

The number of bytes for each basic type on my computer is as follows:

Struct A is as follows:

   1: struct A

   2: {

   3:     int i;

   4:     double d;

   5:     char c;

   6:     short s;

   7: };

There are two ways to align data: natural alignment and forced alignment.

Natural alignment

Struct dataNatural alignmentConditions to be met:

1. Assume that the data offset X occupies B as the data type, and X % B = 0 is required.

2. Assume that the number of bytes in the struct is N, and the number of bytes in the struct is B for the data type (such as double in struct A) with the largest number of nodes. N % B = 0.

Now analyze struct

Int I; int type occupies 4 bytes, the first data of the struct, so the offset is 0 (The unit of offset is byte), which can be divisible by 4, meeting condition 1

Double d; double type occupies 8 bytes, the offset is 4, can not be divisible by 8, does not meet the condition 1, so the offset is moved to 8, so as to meet the condition 1.

Char c; char type occupies 1 byte, offset is 16, can be divided by 1, meet condition 1.

Short s; the short type occupies 2 bytes and the offset is 17. It cannot be divided into 2 and does not meet condition 1. Therefore, the offset is moved to 18 to meet condition 1.

To sum up, the offset of I is 0, the offset of d is 8, the offset of c is 16, and the offset of s is 18.

Assume that the starting address of struct A is Addr, and the memory address of I, d, c, and s is the offset of Addr +.

The total size of all the bytes should be 20 bytes, that is, the memory space occupied by struct A should be (the starting address of struct ~ Starting address of struct A + 19 ). However, condition 2 is not met. In struct A, the maximum number of data segments is double, which occupies 8 bytes. Therefore, the total number of bytes is 24. How can we verify this? You can declare A struct array a A [2];If struct A really occupies 24 segments, the value of & a [1]. I-& a [0]. I should be 24.In other words, after the short s occupies two bytes, it also occupies 4 bytes of memory address to maintain data alignment. & a [1]. i-& a [0]. I = 6. There is a picture with the truth:

Two points observed

1. The memory address of each data is the starting address of struct A (0x002afbac) + the corresponding offset.

2. & a [1]. I-& a [0]. s = 6 to verify that the content in the bold words above is true.

Forced alignment

Required for forced alignment # pragma pack

   1: #pragma pack(1)

   2:  

   3:     struct B

   4:     {

   5:         int i;

   6:         double d;

   7:         char c;

   8:         short s;

   9:     };

  10: #pragma pack()

Ignore the specific features of # pragma pack. You only need to know that it has a parameter.

StructForced alignmentTo meet:

1. assume that the data offset X and the data type count b1, # The pragma pack (b2) parameter is b2, then X % (min (b1, b2) must be satisfied )) = 0.

2. assume that the total number of bytes in the struct is N, and the number of bytes in the struct that occupies the largest number of segments (for example, double in struct A) is B1. # The pragma pack (b2) parameter is B2, so it should meet

N % (min (B1, B2) = 0.

# The role of pragma pack (n) is embodied in condition 1 and 2, and also reflects the difference between forced alignment and natural alignment.

Now we analyze the total number of bytes occupied by struct B (the only difference with struct is that pack is added:

Int I; offset is 0, which can be divided into min (4, 1 ).

Double d; the offset is 4 and can be divisible by min (8, 1)

Char c; the offset is 12 and can be divisible by min (1, 1)

Short s; the offset is 13 and can be divisible by min (2, 1)

Therefore, the total number of bytes is 15. In addition, 15 minutes (8, 1) can be divisible to 2. Therefore, the total number of bytes is 15. See the truth:

Summary

Natural alignment actually has a default pack. In my computer, its default value is 8 (you can query the default value through # pragma pack (show, in the warning column), that is, the natural alignment and

   1: #pragma pack(8)

   2:  

   3:     struct A

   4:     {

   5:         int i;

   6:         double d;

   7:         char c;

   8:         short s;

   9:     };

  10: #pragma pack()

Is equivalent.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.