Reprint: #pragma pack (push,1) & #pragma pack (POP)

Source: Internet
Author: User

1 intro

In the program, sometimes when we define the structure, we use #pragma pack (push,1) & #pragma pack (POP) similar code to wrap the structure together.

The general form is as follows:

#pragma pack (push,1);

struct A

{

} ;

#pragma pack (POP);

What is the purpose of doing this?

Note: The following content is from the network.

2 #pragma pack Introduction

#pragma pack is a way to specify how the data is aligned in memory.

In C, a struct is a composite data type whose constituent elements can be variables of basic data types (such as int, long, float, and so on) or data units of composite data types (such as arrays, structs, unions, etc.). In structs, the compiler allocates space for each member of the structure according to its natural bounded (alignment) condition. Each member is stored sequentially in memory in the order in which they are declared, and the address of the first member is the same as the address of the entire structure.

Example 1:

struct sample
{
Char A;
Double b;
};

If you do not enclose #pragma pack (1) and #pragma pack (), sample is aligned by the compiler's default (the size of the largest member). That is, by 8-byte (double) alignment, sizeof (sample) ==16. Member Char a takes up 8 bytes (7 of which are empty bytes)

If #pragma pack (1) is used, sample ==9 is aligned to sizeof (sample) by 1 bytes. (No empty bytes)

Example 2: The following structure for each member space allocation:
struct test
{
Char X1;
Short X2;
Float X3;
Char x4;
};
The first member of the X1 structure, whose offset address is 0, occupies the 1th byte. The second member, X2, is of type short, and its starting address must be 2-byte bound, so the compiler fills an empty byte between X2 and X1. The third member of the struct, X3, and the fourth member X4, fall exactly on their natural-bound addresses, and do not require additional padding bytes in front of them. In the test structure, the member X3 requires a 4-byte pair of bounds, which is the largest bounded element required by all members of the struct, and thus the natural bounds of the test structure is 4 bytes, and the compiler fills 3 empty bytes after the member X4. The entire structure occupies 12 bytes of space.Change the default byte alignment for the C compiler
By default, the C compiler allocates space for each variable or data unit according to its natural boundary conditions. In general, the default boundary conditions can be changed by the following method:
· Using the pseudo-directive #pragma pack (n), the C compiler will be aligned in N bytes.
· Use pseudo-Directives #pragma pack () to cancel custom byte alignment.

In addition, there are one of the following ways:
· __attribute ((aligned (n))) that aligns the members of the structure to the N-byte natural boundary. if the length of a member in the structure is greater than n, the length of the maximum member is aligned.
· __ATTRIBUTE__ ((packed)), cancels the optimization alignment of the structure during compilation, aligned according to the actual number of bytes consumed.

Above n = 1, 2, 4, 8, 16 ... The first approach is more common.

3 Application Examples

In network protocol programming, data packets of different protocols are often processed. One way is to get a variety of information through the method of pointer offset, but it is not only complicated programming, and once the protocol changes, the program will be more cumbersome to modify. After understanding the compiler's principle of allocation of structure space, we can use this feature to define our own protocol structure, and access the members of the structure to obtain various information. This not only simplifies programming, but even if the protocol changes, we only need to modify the definition of the protocol structure, other programs without modification, save time and effort. The following is an example of the TCP protocol header, which describes how to define the protocol structure. The protocol structure is defined as follows:

#pragma pack(1)Align in 1-byte fashion

struct Tcpheader
{
Short Srcport;16-bit Source port number

Short Dstport;16-bit Destination port number

int Serialno;32-bit serial number

int Ackno;32 Digit Confirmation Number

UnsignedChar Haderlen: 4;4-Bit header length

UnsignedChar Reserved1: 4;Keep 4 bits in 6 bits

UnsignedChar Reserved2: 2;Keep 2 bits in 6 bits

UnsignedChar URG: 1;
UnsignedChar ACK: 1;
UnsignedChar PSH: 1;
UnsignedChar RST: 1;
UnsignedChar SYN: 1;
unsigned char FIN : 1;
Short windowsize; //16-bit window size

Short tcpchksum; //16-bit TCP test and

Short urgentpointer; //16-bit emergency pointer

};
#pragma pack()

Transferred from: http://blog.csdn.net/mannhello/article/details/5384431

Reprint: #pragma pack (push,1) & #pragma pack (POP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.