C + + memory byte alignment detailed

Source: Internet
Author: User

This article refer to http://blog.csdn.net/arethe/article/details/2548867 One, what is alignment, and why to align:

1. The memory space in modern computers is divided by Byte, and in theory it seems that access to any type of variable can start at any address, butThe reality is that when accessing a particular variable, it is often accessed at a specific memory address, this requires that all types of data are arranged spatially in accordance with certain rules, rather than sequentially one-by-one emissions, which is aligned.

2.the role and cause 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. Other platforms may not, but the most common is the loss of access efficiency if data storage is not aligned as required for its platform. For example, some platforms each read from the even address, if an int (assuming 32-bit) if it is stored at the beginning of the even address, then a reading period can be read out, and if stored in the location of the odd address, it may require 2 read cycles, and two read the results of the high and low Byte is pieced together to get the int data. Obviously, the reading efficiency is much lower. This is also the game of space and time.
Second, the realization of alignment normally, when we write a program, we don't need to consider alignment issues. The compiler chooses the alignment strategy for the target platform for us. Of course, we can also notify the compiler to pass the precompiled instructions and change the alignment of the specified data.
However, because we generally do not need to care about this problem, because the compiler to the data storage is aligned, and we do not understand, often will be confused about some problems. The most common is the sizeof result of the struct data structure, unexpectedly. To do this, we need to understand the alignment algorithm. The alignment algorithm:  For char type data, its own alignment value is 1, for the short type is 2, for the int,float,double type, its own alignment value is 4, the unit byte. There are four concept values in this: 1) the self -aligning value of the data type : is the self-aligning value of the underlying data type that was confessed above. 2) The self-aligning value of a struct or class : The value that is the largest of its members in its own alignment value . 3) specifies the alignment value :The specified alignment value when the #pragma pack (value) is values. 4) valid alignment values for data members, structs, and Classes : Their own alignment values and the smaller values in the specified alignment values .   with these values, we can easily discuss the members of a specific data structure and its own alignment.   The valid alignment value n is the value that is ultimately used to determine how the data is stored, most importantly. A valid alignment value of n means "align on n", which means that the data "holds the starting address%n=0", data variables are emitted in a defined order of precedence. The starting address of the first data variable is the starting address of the structure. The member variables of the struct are aligned to the emission, and the structure itself is rounded according to its own valid alignment value ( The total length of a struct member variable is required to be an integer multiple of the struct's valid alignment value ( the member variable itself max) and the smaller ), with the following example, it is not difficult to understand the value of several examples above. because of the different platforms and compilers, I use the GCC version 3.2.2 compiler (32-bit x86 platform) as an example to discuss how the compiler aligns the members of the struct data structure.   1. The structure is defined as follows:
struct A{
int A;
Char b;
Short C;
};
struct A contains a 4-byte-length int, a 1-byte length char, and a 2-byte length of short data. So the space used for a should be 7 bytes. But because the compiler wants to align the data members spatially.
so using the sizeof (Strcut A) value is 8. 2, now the structure of the bodyadjust the order of member variables.
struct B{
Char b;
int A;
Short C;
};
This is also a total of 7 byte variables, but sizeof (struct B) has a value of 12. 3, below we usepre-compilation directives#pragma pack (value)to tell the compiler to use ourSpecifies the alignment value to replace the default.
#progma Pack (2)/ * Specify to align by 2 bytes * /
struct C{
Char b;
int A;
Short C;
};
#progma Pack ()/ * Cancel the specified alignment, restore the default alignment */
The sizeof (struct C) value is 8.
4. Modify the alignment value to 1:
#progma Pack (1)/* Specify 1-byte alignment */
struct D {
Char b;
int A;
Short C;
};
#progma Pack ()/* To cancel the specified alignment, restore the default alignment */
the sizeof (struct D) value is 7.

C + + memory byte alignment detailed

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.