Recently, I debugged the network server program and wrote a small client program to test the program. I found that the service program was unwrapped incorrectly. After debugging, it is found that the client's protocol header size and the server's protocol header size are inconsistent. The reason is that # pragma pack (1) is added on the server, but not on the client.
I have never touched on this compilation macro before. Now I will study it carefully.
First, google ~~
It turns out that the # pragma pack has several forms. What I have come into contact with is # pragma pack (n), that is, the variable is aligned in n Bytes.
Variable alignment is different in each system. The default alignment method can effectively increase the speed of cpu finger fetch, but may waste some space. Using # pragma pack (1) in network programs, that is, variable tightening can not only reduce network traffic, but also be compatible with various systems and avoid unpacking errors due to different System Alignment Methods.
Now that we understand the concepts and advantages, let's test them ~
Platform: CPU-Pentium E5700 memory-2G
1. Operating System: ubuntu 11.04 32bit Compiler: G ++ 4.5.2
2. Operating System: windows xp Compiler: VS2010
First look at the first test.
The memory size occupied by the struct in different environments above in normal conditions and condition.
1 struct pack {
2 int I;
3 short s;
4 double d;
5 char c;
6. short f;
7}
Test results:
1:
2:
Test result analysis:
It can be seen that the size of the structure after compression is 15, which is the sum of the built-in types of the structure. However, by default, the size of the struct is a multiple of the number of aligned bytes. Pack in ubuntu only requires 20 bytes, while windows requires 24 bytes. This is because ubuntu is 4-byte aligned, while windows is aligned with the maximum number of built-in bytes. In the structure, the maximum built-in type is double, and the size is 8 bytes. Their alignment in memory is as follows:
1:
2:
Note that the internal alignment types are both 2-byte alignment.
Conclusion: by default, the linux operating system is 4-byte aligned, while the windows operating system is aligned with the largest built-in type.
Second test
The size of a structure containing another struct.
The internal struct is
1 struct pack {
2 short s;
3 double d;
4}
The external struct is
1 struct complex _ pack {
2 char c;
3 struct pack s;
4 double d;
5 };
We have four situations:
1. pack compression and complex _ pack Compression
2. pack compression, complex _ pack default
3. pack by default, complex _ pack tightening
4. pack default, complex _ pack default
The following are arranged in this order.
Test Results
1:
2:
Test result analysis:
In two operating systems, except for the first case-both the internal and external structures are tight-the same, the other three cases are different. We can plot the condition of the struct in the memory based on the offset. The first case is omitted.
1:
2:
Conclusion: # The pragma pack only affects the alignment of the variable of the current struct and does not affect the arrangement of struct variables in the struct. Or # The scope of The pragma pack is only one layer. In the third case, the internal structure is normal and the external structure is tight. We can conclude that the alignment of the structure is calculated by offset.
There is another problem that cannot be solved. Why is the internal structure offset of 1 in the second case? Not 4 or 8?