This parameter is used by the compiler for the relevant byte alignment structure is provided, #pragma pack specifying data alignment in memory.
#pragma pack (n) action: C compiler aligned in n bytes.
#pragma pack () action: Cancel Custom byte alignment.
#pragma pack (push, 1) effect: refers to the original alignment of the push, and set the new alignment is set to a byte alignment
#pragma pack (pop) effect: to restore alignment status
So visible, adding push and pop can be aligned restored to its original state, rather than the default compiler can be said that the latter better, but a lot of the time difference is small
Such as:
#pragma pack (push) // save aligned state
#pragma pack (4) // set to 4-byte alignment
Corresponds #pragma pack (push, 4)
#pragma pack (1) effect: to adjust the boundary structure is aligned to let a byte alignment; <1 that the structure of byte-aligned>
#pragma pack ()
E.g:
1 #pragma pack(1) 2 3 struct sample 4 { 5 char a; 6 double b; 7 }; 8 9 #pragma pack()
Note: If no #pragma pack (1) and #pragma pack () enclosed, the sample alignment (members of the largest size) by the compiler default. I.e., 8 bytes (double) aligned, sizeof (sample) == 16 char a member holding eight bytes (seven of which are null bytes);. If using #pragma pack (1), according to the sample 1 byte alignment sizeof (sample) == 9. (no null bytes), comparing it to save space, and some fields may also make the structure easier to control.
Applications
In the network protocol programming, often deal with data packets of different protocols. One method is to obtain various kinds of information by means of a pointer offset, but this is not only complicated programming, and once the protocol changes, it is relatively cumbersome to modify. In the understanding of the principle of distribution compiler construction space, we can use this feature to define your own protocol structure, to obtain a variety of information access by members of the structure. In doing so, not only simplifies programming, and even if the agreement is changed, we also need to define the structure of the modified agreement, without modifying other programs, saving time and effort. TCP protocol headers below to illustrate how to define the protocol structure. Protocol structure which is defined as follows:
1 #pragma pack (1) // 1-byte aligned in accordance 2 TCPHEADER struct . 3 { . 4 Short SrcPort; // 16-bit source port number 5 short DstPort; // 16-bit destination port number 6 int SerialNo; // 32 bit serial number 7 int AckNo; // 32-bit acknowledgment number 8 unsigned char HaderLen: 4; // 4 -bit header length 9 unsigned char Reserved1: 4; // reserved 6 bits 4 10 unsigned char Reserved2: 2; / / reserved 2 6 in . 11 unsigned char the URG:. 1; 12 is unsigned char the ACK:. 1; 13 is unsigned char PSH:. 1; 14 unsigned char the RST:. 1; 15 unsigned char the SYN:. 1; 16 unsigned char the FIN:. 1; 17 short WindowSize; // 16-bit window size of 18 short TcpChkSum; // 16-bit TCP checksum 19 short UrgentPointer; // 16 bit urgent pointer 20}; 21 #pragma pack ()
Transfer: http://blog.csdn.net/vblittleboy/article/details/6935165
#pragma pack (n) action: C compiler aligned in n bytes.
#pragma pack () action: Cancel Custom byte alignment.
#pragma pack (push, 1) effect: refers to the original alignment of the push, and set the new alignment is set to a byte alignment
#pragma pack (pop) effect: to restore alignment status
So visible, adding push and pop can be aligned restored to its original state, rather than the default compiler can be said that the latter better, but a lot of the time difference is small
Such as:
#pragma pack (push) // save aligned state
#pragma pack (4) // set to 4-byte alignment
Corresponds #pragma pack (push, 4)
#pragma pack (1) effect: to adjust the boundary structure is aligned to let a byte alignment; <1 that the structure of byte-aligned>
#pragma pack ()
E.g:
1 #pragma pack(1) 2 3 struct sample 4 { 5 char a; 6 double b; 7 }; 8 9 #pragma pack()
Note: If no #pragma pack (1) and #pragma pack () enclosed, the sample alignment (members of the largest size) by the compiler default. I.e., 8 bytes (double) aligned, sizeof (sample) == 16 char a member holding eight bytes (seven of which are null bytes);. If using #pragma pack (1), according to the sample 1 byte alignment sizeof (sample) == 9. (no null bytes), comparing it to save space, and some fields may also make the structure easier to control.
Applications
In the network protocol programming, often deal with data packets of different protocols. One method is to obtain various kinds of information by means of a pointer offset, but this is not only complicated programming, and once the protocol changes, it is relatively cumbersome to modify. In the understanding of the principle of distribution compiler construction space, we can use this feature to define your own protocol structure, to obtain a variety of information access by members of the structure. In doing so, not only simplifies programming, and even if the agreement is changed, we also need to define the structure of the modified agreement, without modifying other programs, saving time and effort. TCP protocol headers below to illustrate how to define the protocol structure. Protocol structure which is defined as follows:
1 #pragma pack (1) // 1-byte aligned in accordance 2 TCPHEADER struct . 3 { . 4 Short SrcPort; // 16-bit source port number 5 short DstPort; // 16-bit destination port number 6 int SerialNo; // 32 bit serial number 7 int AckNo; // 32-bit acknowledgment number 8 unsigned char HaderLen: 4; // 4 -bit header length 9 unsigned char Reserved1: 4; // reserved 6 bits 4 10 unsigned char Reserved2: 2; / / reserved 2 6 in . 11 unsigned char the URG:. 1; 12 is unsigned char the ACK:. 1; 13 is unsigned char PSH:. 1; 14 unsigned char the RST:. 1; 15 unsigned char the SYN:. 1; 16 unsigned char the FIN:. 1; 17 short WindowSize; // 16-bit window size of 20}; 18 short TcpChkSum; // 16-bit TCP checksum 19 short UrgentPointer; // 16 bit urgent pointer 21 #pragma pack ()
Transfer: http://blog.csdn.net/vblittleboy/article/details/6935165