Description: With regard to byte alignment, many people gave the answer by setting the #pragma pack (1) to operate. However, in situations where space usage is not a concern, or if the protocol package is not being sent, this setting is not performed in order for the program to run faster.
The following are the default byte alignments
1. In a struct, the number of bytes used in the alignment is the maximum amount of bytes that are searched for keywords in the struct (the default minimum is 4 bytes)
Like what:
struct Node {
int A;
Double b;
};
The size of sizeof (Node) is 16 bytes instead of the so-called 12 bytes, because the number of bytes in the struct keyword (double) is 8 bytes, so the size is byte-aligned.
If this is the case:
struct Node {
int A;
Double b;
char c;
}
Then the size of sizeof (Node) is 24 bytes.
2, regarding the member function pointer occupies the space size, the concrete can see the following link
Http://www.linuxeden.com/html/news/20141206/157482.html
Note that if this is the case:
Class Base;
typedef (BASE::* Func) ();
Then func takes up 16 bytes
If
Class Base;
Class base{};
typedef (BASE::* Func) ();
Then Func takes up only four bytes. This means that as long as the class Base is defined in a typedef (BASE::* Func) (), it will simply take up a simple corresponding number of bytes.
3. What happens if the member function pointers are also put into the structure?
Declare a class here
Class Base;
typedef (BASE::* Func) ();
Through the 2nd link we know here the size of sizeof (Func) is 16 bytes
So here's an example
struct Node {
int A;
func func;
Double b;
}
How much space does sizeof need to occupy? The answer is 8+16+8 = 32 bytes. Since the structure is calculated with the largest footprint, why is it 32 bytes instead of 48 bytes? In fact, the alignment is the keyword (double) that is defined by C + +, as I said above. So it takes only 32 bytes.
A summary of the size of bytes in VS