Memory alignment in VC ++ and memory alignment in vc
We often see the problem of sizeof (A) value, where A is A struct, class, or consortium.
In order to optimize CPU access and memory and reduce memory fragmentation, the compiler sets some rules for memory alignment. However, different compilers may have different implementations. This article only applies to VC ++ compilers. The IDE here is VS2012.
# Pragma pack () is a preprocessing that indicates memory alignment. Layout control # pragma, providing unconventional control flow information for the Compilation Program.
*************/
The size of the struct is an integer multiple of the smaller number of processor digits and the maximum number of bytes occupied by data elements in the structure.
For example, assume that the number of processor digits is n, and the maximum number of data elements in the structure is m.
The processor is 32-bit, n = 4; the maximum data type in the Structure Body is short, m = 2; n> m; the struct size is an integer multiple of m, and vice versa.
Note: although some operating systems are 64-bit, the compiler is 32-bit, and the number of digits is 32.
1 class A{2 int a;3 char b;4 short c;5 };
Sizeof (A) is 8, an integer multiple of 4.
1 struct B{2 short a;3 short b;4 short c;5 };
Sizeof (B) is 6, an integer multiple of 2 (sizeof (short.
Note: There is only one difference between the struct and the class in C ++, that is, the struct member is public by default, and the class is private by default.
class X{public: double a; float b; int c; char d;};
Sizeof (X) is 20, an integer multiple of 4 (number of processor digits.
/******** # Pragma pack (n )*************/
# In The pragma pack (n), n is 4 by default, that is, 32 processor bits, but we can define its size by ourselves.
#pragma pack(1)class A{public: int a; char b; short c;};
In this case, sizeof (A) is 7, which is an integer multiple of 1 (# pragma pack (1.
#pragma pack(1) class X{ public: double a; int b; short c; char d; };
Sizeof (X) is 15, an integer multiple of 1 (# pragma pack (1.
#pragma pack(4) class X{ public: double a; int b; short c; char d; };
Sizeof (X) is 16, an integer multiple of 4 (# pragma pack (4.
#pragma pack(8) class X{ public: double a; int b; short c; char d; };
Sizeof (X) is 16, which is an integer multiple of 8 (# pragma pack (8) or sizeof (double.
/************** Memory alignment **************/
The memory address of the data element in the struct is determined by two factors.
1: # n in The pragma pack (n), 2: the number of bytes occupied by the element type, sizeof (type), take a smaller one of the two, the offset from the Element Memory Address to the starting address of the struct or class is an integer multiple of the decimal number.
For example, the default value of # pragma pack (n) is 4, and the following struct is available:
struct A{ int a; char b; short c;};
The offset between the start address and the start address of a is 0, which is an integer multiple of sizeof (int.
The offset between the start address and the start address of B is 4, which is an integer multiple of sizeof (char.
The offset of the starting address of c from the starting address of the struct is 5, not an integer multiple of sizeof (short). Therefore, the starting address offset of c is 6 instead of 5.
The output address of a, B, and c is
0043FD68
0043FD6C
0043FD6E
We can see that the starting address of c is 2 bytes larger than the starting address of B, and B occupies 2 bytes. This is because the type of c is short and the size is 2, the default value of n is 4 and sizeof (short) <n, so the offset should be an integer multiple of 2. Here is 6.
How to Set memory alignment for mingw
Specifically, the internal memory of the struct is aligned. The complete method is:
# 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
GCC may not support progma! However, the C ++ compiler, such as g ++, is supported. You can try it.
......
How to clear memory alignment
The result is correct.
Because pointer is not involved, the 32-bit and 64-bit values are not considered as 32-bit values. In C ++, int Is 4 bytes.
The memory alignment of a struct is aligned to the maximum type in the struct during compilation, which is an integer multiple of the maximum number of bytes. If the memory occupied by a member type is smaller than the maximum type, alignment is performed by adding the memory occupied by the next (or the next) member. The memory must be smaller than or equal to the memory occupied by the maximum type. If it is smaller than the memory occupied by the maximum type, the alignment will be extended to the maximum type in the struct;
However, if the type of the next member is already aligned with the maximum type in the struct, the memory (and) occupied by the previous Member will be extended to the maximum type alignment in the struct.
Structures that involve the in-place domain (such as the first two structs) are difficult to align and involve different compiler problems. Different compilers have different processing methods, for example, if C ++ is adjacent to different types without compression, Dev-C ++ is compressed and saved.
The following three examples are analyzed respectively:
1.
Struct Date
{
Int year: 20; // 20 bits
Int month: 6; // 6 bits
Char day: 6; // 1 byte 8 bits
}; // The sizeof (Date) is 8
The first two Members have a total size of 26 bits, which is smaller than the 4-byte 32 bits of the int type. Although the third member is also 6 bits, the sum is equal to 32 bits, but because the types are different, the memory occupied by the first two members is extended to the largest int type alignment to the struct, which occupies 4 bytes;
The Third Member is char type 6 bits. It also needs to correspond to the largest int type of the struct and also occupies 4 bytes of memory;
8 bytes in total;
Note: 8 bytes. This is the case in VC. If Dev-C ++ is used, this struct only occupies 4 bytes.
2.
Struct Date
{
Int year: 20; // 20 bits
Int month: 6; // 6 bits
Int day: 6; // 6 bits
}; // The sizeof (Date) is4
In this example, the size of the three members is 32 bits, exactly 4 bytes, and of the same type. The Compiler compresses and saves the size, alignment the maximum type of int In the struct, and will not be extended, it only occupies 4 bytes of memory;
Even if a member with only 1 bit is added later, the memory occupied by the struct will be extended to 8 bytes, aligned with the int type.
3.
Struct Date
{
Int year; // 4 Byte
Int month; // 4 Byte
Int day; // 4 bytes
}; // The sizeof (Date) is 12
The three members are of the int type, and they are also aligned with the four bytes of the int type, so they can be added, occupying 12 bytes of memory in total.