About structure alignment in C language

Source: Internet
Author: User
Tags expression integer

(1) What is the byte alignment

If a variable occupies n bytes, the starting address of the variable must be divisible by n, that is, the starting address% n = 0, and for the struct, this n takes the data type of the member species that occupies the largest space.

(2) Why do you want to align bytes

Memory space is divided by Byte, in theory, access to the memory space can start at any address, but in fact different architectures in order to improve the speed of access to memory, it is specified for certain types of data can only start from a specific starting point of access. This determines that a variety of data types can only be stored in the memory space according to the corresponding rules, and not in the order of one after another.

For example, some platform access memory addresses start with an even address, for an int (assuming 32-bit system), if stored from an even address, such a read cycle can read the int data, but if the address from the odd address to store, it will take two reading cycles, And the high and low byte of two read results are pieced together to get the int data, which obviously reduces the efficiency of reading.

(3) How to align bytes

Each member is aligned to the smaller of its type's alignment parameters (usually the size of this type) and the specified alignment parameter (the default value is not specified). And the length of the structure must be an integer multiple of all the alignment parameters used, not enough to fill the byte.

This rule is a bit bitter, you can break down this rule, the meaning of the first half of the sentence after the alignment value is compared with the specified alignment value, where the alignment value is obtained as follows:

1. The data type's own alignment value is: For char data, its own alignment value is 1, for the short type is 2, for int, long, float type, its own alignment value is 4, and for the double type its own alignment value is 8, in bytes.

2. The structure itself is aligned to the value: the value of its members to the largest value in its own alignment.

Where the specified alignment value is obtained as follows:

The value value of the specified alignment when #pragma pack (value).

The default value is taken if not specified.

The second half of the sentence means primarily for the length of the structure, because it has only one alignment parameter for the member of the data type, and its length is 1 time times that of the alignment parameter. For a struct, it may use a variety of data types, and the sentence is translated into alignment rules: Each member's starting address% itself is aligned to the value = 0, and if not equal to 0, the byte is first filled until the expression is set.

In other words, for a struct, the structure is mapped in the order in which the memory is stored in the following rules:

(a) Each member's starting address% of each member's own alignment value = 0, if not equal to 0, first fill the byte until the expression is established;

(b) The length of the structure must be an integer multiple of the structure's own alignment value, not enough to fill the empty byte.

As an example:

#pragma packs (8)
struct a{
Char A;
Long B;
};

struct b{
Char A;
struct A b;
Long C;
};

struct c{
Char A;
struct A b;
Double C;
};

struct d{
Char A;
struct A b;
Double C;
int D;
};

struct e{
Char A;
int b;
struct A C;
Double D;
};

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.