One, memory alignment
(a), why is there memory alignment?
1, in order to improve the performance of the program, data structure (especially the stack) should be as far as possible on the natural boundary alignment. The reason is that in order to access unaligned memory, the processor needs two accesses, and access to the aligned memory is only needed once. This approach is referred to as "space-for-time" in many of the issues that require time complexity.
650) this.width=650; "Src=" http://img.blog.csdn.net/20160514161604175?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "alt=" Center "/>
2, memory alignment can increase the portability of the program, because not all platforms are free to access the memory, some platforms can only read memory at a specific address everywhere.
In general, memory alignment is a compiler thing, we do not need to consider, but some issues still need to be considered, after all, C + + is the direct operation of memory language, need to understand the program in-store distribution and operation principle.
(b), memory alignment: So how to align?
Alignment principle: The starting position of data storage is an integer multiple of its size.
Example: Memory starts at 0 address.
Char1 bytes, so it can be stored to a multiple of 1 at the address.
Short2 bytes, so it can be stored to a multiple of 2 at the address.
int is 4 bytes, so it can be stored at a multiple of 4 when it is aligned.
The double is 8 bytes, so it can be stored at a multiple of 8 when it is aligned.
Now that you understand the principle of memory alignment, let's look at the structure body alignment.
(iii) to understand several concepts before understanding the structure body alignment:
1, the default alignment number: In VS memory alignment number default is 8,linux is 4. You can modify the default alignment number by #program pack ().
2, offset: The position relative to the starting position. For example, the starting position is 2, then 2 is the 0 offset, and 3 is the 1 offset.
650) this.width=650; "Src=" http://img.blog.csdn.net/20160514161809657?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "alt=" Center "/>
3, Alignment number: The size of the variable itself and the minimum value in the default alignment number. Assuming that the default alignment number is the 8,int type, the number of alignments is 4. Because the int size is 4, less than 8
Second, the structure in vivo alignment principle:
1, structure, or union of data members, the first member is placed at 0 offset, and each subsequent data member is placed into an integer multiple offset of its own alignment number.
2. The size of the struct must be an integer multiple of the maximum number of alignments.
Example 1:
struct STU
{
char c; The alignment number is 1
Short B; The alignment number is 2
Double D; The alignment number is 8
int i; The alignment number is 4
};
650) this.width=650; "Src=" http://img.blog.csdn.net/20160514161848585?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "alt=" Center "/>
Example 2:
struct STU
{
char c; The alignment number is 1
Short B; The alignment number is 2
struct A
{
Double D; The alignment number is 8
};
int i; The alignment number is 4
}s;
The size of the nested structure, the method of analysis is the same, the maximum number of alignments is 8,sizeof (s) = 24.
Iii. Custom Types
(i), structure declaration
1, no label, incomplete statement. It also defines a variable.
struct
{
Charc;
SHORTB;
Inti
}T1;
2, there is a label declaration, but does not define a variable declaration.
struct A
{
Charc;
SHORTB;
Inti
};
Define a variable struct a *s1;
Note that in the same program, 1 or 22 structures are declared at the same time, and 1, 22 structures are considered to be of different types. So the s1=&t1 is wrong.
3, a label declaration, but also define a variable.
struct A
{
Charc;
SHORTB;
Inti
}T3;
4, the Declaration of the structure of the body weight named a.
typedef struct A
{
Charc;
SHORTB;
Inti
}a;
5. First Chicken or egg
struct B//no matter which is put in front of the wrong
{
Structaa;
}s;
Structa
{
STRUCTBB;
};
If two structures are nested with each other, an incomplete declaration of one of the structures is required at the time of declaration.
Structa;
struct B
{
Structaa;
}s;
Structa
{
STRUCTBB;
};
(b), the initialization of the structure:
For example:
typedef struct A
{
Charc;
SHORTB;
Inti
}a;
As1 = {' C ', 2, 4};
(c), the self-reference of a struct: (the self-reference of a struct is usually used in a linear structure of a linked list)
1, the wrong self-reference way, it is easy to understand, the structure of the structure of the body, so that the cycle continues. (There used to be a temple, there was an old monk in the temple, the old Monk told the story to the Little monk ... ^v^)
typedef struct A
{
Intdata;
Structan; Dead loop
}a;
2, the wrong reference only, because the struct is renamed to A is after the reference.
typedef struct//The label cannot be omitted when the struct is self-referencing.
{
Intdata;
an; You must use the full struct name
}a;
3, the right way
typedef struct A
{
Intdata;
Structa *n; Declare a struct pointer with the full struct name,
}a;
(iv), the efficiency of the structure to do parameter transfer:
When the structure is large, when the struct is passed as a parameter, we pass its address, which improves efficiency, and if you don't want to change the structure content, add the const on the formal parameter.
(v), flexible arrays:
The last member in the struct allows an array of unknown size, which consists of a flexible array (at least one member variable before a flexible array)
typedef struct A
{
Inti
Char a[];
}a;
Structure containing flexible arrays large: Such a structure, its size does not include a flexible array, so sizeof (A) = 4; The size of the empty structure is 1;
(vi), bit segment (bit field):
1, Concept: In a struct in a bit units to specify the actual size of the memory of the member, such as bits of the member we call the bit segment, bit segment is a special structure, the declaration of bits and any ordinary struct member declaration similar to the following:
struct bit segment structure body name
{
Unsigned bit segment name: bit segment length;
Unsigned bit segment name: bit segment length;
....................
Unsigned bit segment name: bit segment length;
} bit segment struct variable name;
However, with two exceptions, the first bit segment member must be declared as int, unsigned int, signed int,. Second, after the member is a colon and an integer that specifies the number of bits occupied by that bit segment. (It is also possible to find the char type after actual validation, but note that int and char cannot be used in a bit segment).
2, the use of bits should be noted that:
1. Members in a bit-segment struct cannot use arrays and pointers, but struct variables can make arrays or pointers.
2. Because both arrays and pointers are in bytes, you cannot use & to get the address of a bit segment.
3. The bit segment does not support porting.
Example 1: Declaring a bit segment, let's first analyze how he is stored in the computer (an unsigned int is 4 bytes).
struct TAGAAA
{
unsigned int a:1;
unsigned int b:2;
unsigned int c:6;
unsigned int d:4;
unsigned int e;
}aaa_s;
650) this.width=650; "Src=" http://img.blog.csdn.net/20160514164901090?watermark/2/text/ Ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity/center "alt=" Center "/>
Thus we can understand the advantages of the bit segment, originally defined 5 members, requires 5 storage units, but the use of bits after only 4 storage space is sufficient.
3. Advantages:
But its members are fields of one or more bits, and these different lengths of fields are actually stored in one or more shaping variables, and his advantage is the ability to store data in fewer memory units. Bit segments can be output in shaping form.
Example 2:
struct TAGAAA
{
unsigned int a:1;
unsigned int:2; The variable is not declared, but it refers to the size of the anchor segment, called a placeholder.
unsigned int c:6;
unsigned int d:4;
unsigned int e; Does not refer to the size of the anchor segment, default to the size of its own type
}aaa_s;
(vii), joint
1, the joint statement:
Typedefuniona
{
Inti
Charc;
}a;
2, the characteristics of the joint:
The same block space is shared between federated members. The size of the union equals the maximum variable size of the memory in the member. Can be used to measure the size of the end.
(eight), enumeration:
1. Statement:
Typedefenuma
{
Zero
One
Both
}a;
If an enumeration member is not initialized, the default enumeration member increments from 0
Attention:
1. In the same program, you cannot declare enum types with the same name
2. In the same program, enumeration members of different enumeration types cannot have the same name.
3, the size of any enumeration is 4
2. The difference between an enumeration and a # define identifier:
1. #define identifiers are simple replacements during precompilation. Enumeration type determines its value at compile time.
2. Enumeration constants can be debugged, #define identifiers are not available.
3. Enumeration can define a large number of enumerators at once.
This article from the "11132019" blog, reproduced please contact the author!
Memory alignment and custom types