Appreciated custom types (structures, enumerations, joint) of

Structure type

In fact, the structure is well understood, an understanding can understand.
A memory structure of the same or different data type. A structure may represent an entity. You can put the type of data you want to put into a structure.

Sometimes, the very image of the structure. Such a structure may be represented using a dot.
He has a point x, y coordinates. Then we can be encapsulated in a structure inside.

With the structure type, then define the variable is very simple.

definition:

Point struct
{
int X;
int Y;
} p; // declare a type definition of the structure while the variable p

You can also define after the first declaration
struct Point p;

initialization

Indeed, as initialized, and an array of structures, can be assigned at the same time the overall definition, but if you want to define and then assignment, you must assign one by one.

(1), while the assignment defined
struct Point p1 = {3,4};

(2) Assignment defined
struct Point P2;
P2.x =. 3;
p2.y =. 4;

When the present structure is a nested structure, the same evaluation methods and arrays (nesting braces may be utilized).
the Node struct
{
int Data;
struct Point P;
} = {n-10, 3,4- {}};

Self-referential

When you create a structure type, there is a problem of self-referential structures. Self-referencing means is a reference to itself when the internal variable definition of a structure.
the Node struct
{
Int Data;
struct the Node Next;
}
In this case, the C language is not allowed. To define a variable because its size must be clear, that is to say the need for open space. So this case is equivalent to a recursive self-referential no exit (because you know when you define the size of the structure variables)

So in the end structure to achieve self-referential Can it?
Of course it can, but need pointers to achieve.
the Node struct typedef
{
int Data;
struct the Node * Next;
} the Node;

struct Node
{
int data;
struct Node* next;
}

Memory alignment problem

We know that there are all types of data size. Then the structure type of size is how much?
This time on issues related to the alignment of the memory structure.
Encapsulation structure will inevitably lead to different data types inconvenient access element.
So it was a memory alignment.

What is memory alignment? You only need to remember memory alignment structure is to save time for data access method of design of a memory.
This storage method is to buy time by sacrificing space.

With this method after the size of the structure becomes " uncertain " the.
For example,
the memory size of a request following structure
struct S1
{
char C1;
int I;
char C2;
};
? The sizeof ((struct S1))
Here Insert Picture Description
Why is 12! ?
This is because the memory alignment.
Computer when you deposit directly into char c1 (one byte)
then continue 3 bytes continue to exist behind c1, or open up a new 4-byte space in the newly opened store in the int i (4 bytes) keep it local?
The answer is definitely the latter, because it is the memory alignment.
Before explaining memory alignment, to explain the structure and alignment of the offset number.
Offset is the difference between the first address and the address 0 of the memory when the data. So just int offset i is 4.
Alignments generally refers to the size of the data itself. int alignment number is 4, char 1.

So what structure to meet the rules memory alignment?
PS: When a variable is first stored memory alignment need not be considered.
(1) the first member of the variable offset of 0.
(2) to be aligned to other members of the variable integer multiple of the number of addresses to align itself. Int i present example, the offset position 4 of
an integer multiple (3) The total size of the structure is a maximum alignment (internal data structure that is aligned with the maximum number) (the least common multiple in fact, later speaks)
(4 ) presence of nested structure must also meet the above three.
ps: Number aligned structure maximum alignment of its internal elements.

We now look
struct S1
{
char c1;
int I;
char C2;
;}
Here Insert Picture Description
After saving c1 placing it at the first byte, and three bytes of memory space int i, i representing four bytes , then the last saved char c2 (one byte).
At this time, accounting for a total of 9 bytes, the size of this structure is 9 it?
No, it had to meet the third, 9 is not divisible by 4 (maximum alignment), then 1 is added, can not be divisible by 10, 12, 12 can be finally added. Therefore, the size of the final structure is 12.

Because it aligned in memory, so that different data may also cause the position of different sizes structure.
S1 struct
{
int I;
char C1;
char C2;
}
In this case the size becomes. 8
I 4 bytes, char type two 2 bytes.
At this time, accounting for a total of 6 bytes, but is not divisible by 4 6 (maximum alignment), so the size of 8 on.

Conclusion: When defining the structure should try to define the memory occupied by smaller structures.

Calculate the bit size segment and bit segment

Bit segment

Declarations and structure-bit segment is similar to (but bit segment in order to save space), there are two differences:
the members of (1) a bit segment must be int, unsigned int, signed int (signed int)
(2) Bit after the members section of a colon and a number.
Number indicates how many bits you want to save.
Sterically segment is four bytes in accordance with (int) or a byte (char) allocation. char is actually an integer.
Bit segment space saving is achieved by continuously stored data.
For example,
struct A
{
int a: 14;
int B: 16;
}
bit segment create four bytes (8 * 4 = 32 bits), when a 14-bit memory bits.
Was found to deposit 16 b bits and to be put down, then began to continuously deposit the first 16 bits in 15 bits.
So in fact, this segment accounts for only 4-bit bytes.
Here Insert Picture Description

Uncertainties bit segment

Bit segment there are many uncertain factors, which led to a bit segment is not cross-platform.
The segment member (1) bit in memory is kept from left to right or from right to left deposit is uncertain.
The remaining space is a former member of the (2) position when the next period is not sufficient to save enough for a member. At this point there should be the next member to open up a whole new space, or a former member of the remaining part of the memory space, then deposit a portion of the new space in it. (It is uncertain)

To calculate the bit size segment

In fact, I have previously made a brief introduction.
Storing consecutive bit segment so that the bit segments occupy very little memory.
But also because many uncertain factors lead to its size may be different in different circumstances. (Such as second above)
Here Insert Picture Description
Here Insert Picture Description
it can be seen from the above two shots, my computer when faced with bit storage section, when the second member is not the first member of the remaining storage space complete, will be in the new space storage. (A remaining one bit, able to save B is insufficient, and so, this leading to a bit segment occupies 16 bytes)

+ Joint enumeration

enumerate

See enumeration name known meaning, even if all enumerated one by one.
such as

definition

Day {enum
Mon,
Tues,
Wed,
Thur,
Fri,
the Sta,
the Sun
};
a total of 7 weeks
enum Sex
{
MALE,
FEMALE
};
2 genders

Inside there is an enumeration of constant values are integers.
If no initial value, then it is from 0 increments.
If the definition is assigned, then began to increase from a value assigned.
For example,
Here Insert Picture Description
can only take enumeration constant to variable assignment enumeration, enumeration constants is out of all possible values of enumeration. In fact, assigned to the integer data enumeration is also possible, but which lost the meaning of enumeration. So we generally do not do that!
enum DAY r = a; (right)
enum. 9 DAY = R & lt; (possible but not recommended)
Of course, the enumeration does not necessarily have to include complete.
As long cited the need to use it.

advantage

(1) increase the readability and maintainability of the code
(2) and #de fi ne identifier comparator enumeration has defined the type of inspection, more rigorous.
(3) preventing contamination of the name (package)
(4) to facilitate, once defined a plurality of constants.

joint

Also known union joint. (This is because the union member variables in the shared space)

statement

union Un
{
char c;
int i;
};
sizeof(union Un)?
Here Insert Picture Description

Joint features

(1) union members are sharing the same memory space, so that a size of joint variable, at least the size of the largest member (since at least the joint have to have the ability to save the maximum that member)
(2) joint where each member The first can be seen as a member variable.
the printf ( "% P \ n-", R.C. &);
the printf ( "% P \ n-", & R.I.);
Here Insert Picture Description
Using this feature, we can measure the size of the client computer:
Here Insert Picture Description

Joint size calculations

(1) at least combined size is the size of the largest member.
(2) When the maximum number is not an integer multiple of the size of the largest member is aligned, it is necessary to align the maximum number of integer multiple alignment. short (2 bytes)
Here Insert Picture Description

Published 57 original articles · won praise 11 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_42419462/article/details/94660833