Computer byte alignment

Source: Internet
Author: User

The definition is long at least not less than int,long long, at least not less than long;
In a 32-bit machine, the general long and int are the same, 32,long long is 64;

A detailed explanation of byte alignment in C + + memory

What is byte alignment and why is it aligned?
The memory space in modern computers is divided by Byte, in theory it seems that access to any type of variable can start from any address, but the reality is that when accessing a particular type of variable, it is often accessed at a specific memory address, which requires all types of data to be spatially arranged according to certain rules, Instead of sequentially one by one emissions, that's the alignment.
The effect and reason of alignment: the processing of storage space varies greatly with each hardware platform. Some platforms can only access certain types of data from certain specific addresses. For example, some architectures have an error when accessing a variable that is not aligned, so programming must ensure byte alignment in this architecture. Other platforms may not have this, but the most common is the loss of access efficiency if the data storage is not aligned according to their platform requirements. For example, some platforms read each time from the even address, if an int (assuming 32-bit system) if the location of the place where the even address begins, then a read cycle can be read out of the 32bit, and if it is stored at the beginning of the odd address, it takes 2 read cycles, The 32bit data can be obtained by piecing together the high and low bytes of the two read-out results. Obviously, the reading efficiency is much lower.

Second, please look at the following structure:

struct MYSTRUCT
{
Double dda1;
Char DDA;
int type
};
What happens when you use sizeof for structural mystruct? How much is sizeof (MYSTRUCT)? Perhaps you would ask:
sizeof (MyStruct) =sizeof (double) +sizeof (char) +sizeof (int) =13
However, when you test the size of the structure in the VC, you will find that sizeof (MYSTRUCT) is 16. Do you know why the VC will produce such a result?
In fact, this is a VC on the variable storage of a special treatment. In order to improve the storage speed of the CPU, VC has made the "alignment" processing for the starting address of some variables. By default, the VC specifies that the offset of the starting address for each member variable to be stored relative to the start address of the structure must be a multiple of the number of bytes occupied by the variable's type. The following is a list of common types of alignments (vc6.0,32-bit systems).
Type
Alignment (the offset at which the variable holds the starting address relative to the structure's start address)
Char
The offset must be a multiple of sizeof (char), or 1
Int
The offset must be a multiple of sizeof (int), or 4
Float
The offset must be a multiple of sizeof (float), or 4
Double
The offset must be a multiple of sizeof (double), or 8
Short
The offset must be a multiple of sizeof (short), or 2
Each member variable applies space in the order in which it appears in the structure, and adjusts the position according to the alignment above, and the empty byte VC is automatically populated. At the same time VC to ensure that the size of the structure of the structure of the number of bytes (that is, the structure of the maximum space occupied by the type of the number of bytes), so after the last member variable to request space, also automatically fill the empty bytes as needed.
The following example to illustrate how VC exactly how to store the structure.
struct MYSTRUCT
{
Double dda1;
Char DDA;
int type
};
When allocating space for the above structure, the VC allocates space for the first member DDA1 according to the order and alignment of the member variables, the starting address is the same as the starting address of the structure (just 0 is exactly the multiple of sizeof (double), which takes up sizeof ( Double) = 8 bytes; Then allocate space for the second member DDA, at which point the next assignable address has an offset of 8 for the starting address of the struct, a multiple of sizeof (char), so the DDA is stored at an offset of 8 where the alignment is satisfied, and the member variable occupies sizeof (char) = 1 bytes; Next, allocate space for the third member type, at which point the next assignable address has an offset of 9 for the starting address of the struct, not a multiple of the =4 of sizeof (int), in order to satisfy the constraint on the offset of the alignment. VC automatically fills 3 bytes (this three bytes did not put anything), then the next assignable address for the structure of the starting address of the offset of 12, just a multiple of sizeof (int) =4, so the type is placed at an offset of 12, the member variable occupies sizeof ( int) = 4 bytes, at which time the member variable of the entire structure has been allocated space, the total space occupied is: 8+1+3+4=16, which is exactly the number of bytes of the structure (that is, the maximum space in the structure of the type occupied by sizeof (double) =8) multiples, So there are no vacant bytes that need to be populated. So the size of the whole structure is: sizeof (mystruct) =8+1+ 3+4=16, of which 3 bytes are VC auto-filled, not put anything meaningful.
In the following example, swap the position of the member variable of the above mystruct to make it the following situation:
struct MYSTRUCT
{
Char DDA;
Double dda1;
int type
};
How much space does this structure occupy? In the VC6.0 environment, sizeof (MYSTRUC) can be obtained as 24. According to some principles of allocating space mentioned above, this paper analyzes how VC allocates space for the above structure. (Simple description)
struct MYSTRUCT
{
The Char dda;//offset is 0, which satisfies the alignment and the DDA occupies 1 bytes;
Double dda1;//The offset of the next available address is 1, not sizeof (double) =8
Multiples of 7 bytes to make the offset to 8 (to satisfy the alignment
), so VC automatically fills 7 bytes, dda1 is stored at an offset of 8
Address, it occupies 8 bytes.
int type;//The offset of the next available address is 16, which is the number of sizeof (int) =4
number, which satisfies the alignment of int, so no VC auto-fill is required, type is stored
Placed at an offset of 16, it occupies 4 bytes.
};//all member variables are allocated space, the total size of the space is 1+7+8+4=20, not the structure
The number of section boundaries (that is, the number of bytes in the structure that occupy the largest space in the struct. sizeof
(double) =8), so a multiple of 4 bytes is required to satisfy the size of the structure to
The multiple of the sizeof (double) =8.
So the total size of the structure is: sizeof (MYSTRUC) is 1+7+8+4+4=24. One of the total 7+4=11 bytes is the VC automatically filled, not put anything meaningful.
VC storage of the structure of the special processing does increase the speed of the CPU storage variables, but sometimes it brings some trouble, we also block out the default alignment of variables, we can set the alignment of variables.
The #pragma pack (n) is provided in the VC to set the variable to n-byte alignment. N-byte alignment means that the offset of the starting address of the variable is in two cases: first, if n is greater than or equal to the number of bytes occupied by the variable, then the offset must satisfy the default alignment, and second, if N is less than the number of bytes occupied by the type of the variable, then the offset is a multiple of n, not the default The total size of the structure also has a constraint, in the following two cases: if n is greater than the number of bytes occupied by all member variable types, the total size of the structure must be a multiple of the amount of space occupied by the variable occupying the largest space;
Otherwise, it must be a multiple of n. The following examples illustrate their usage.
#pragma pack (push)//Save alignment status
#pragma pack (4)//set to 4-byte alignment
struct test
{
Char M1;
Double M4;
int m3;
};
#pragma pack (POP)//restore alignment status
The size of the above structure is 16, the following analysis of its storage, the first to allocate space for M1, its offset is 0, to meet our own set of alignment (4-byte alignment), M1 occupies 1 bytes. It then begins allocating space for the M4, where the offset is 1, which requires 3 bytes to be filled so that the offset satisfies a multiple of n=4 (because sizeof (double) is greater than N) and M4 occupies 8 bytes. The M3 is then allocated space, at which point the offset is 12, satisfies the multiple of 4, and M3 occupies 4 bytes. At this point, all member variables have been allocated space, with a total of 16 bytes allocated, which satisfies a multiple of n. If you change the #pragma pack (4) above to #pragma pack (16), then we can get the size of the structure to 24.

Computer byte alignment

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.