Pragma pack (n) Usage

Source: Internet
Author: User

Question:
# Pragma pack (1)
Struct stooges {
Uchar Larry;
Uint Moe [20];
} Arbitrary_structure;
# Pragma pack ()

Msdn: When you use # pragma pack (N), where N is 1, 2, 4, 8, or 16, each structure member after the first is stored on the smaller member type or N-byte boundaries. if you use # pragma pack without an argument, structure members are packed to the value specified by/ZP. the default/ZP Packing Size is/zp8.

Please advise: What does pack (1) mean and what is its purpose.
 
 
Solution 01:
# Pragma pack ([{push | Pop},] [identifier,] [N]) (N -- number of aligned bytes)
It is used to set the number of alignment bytes of your structure, so that your structure becomes more compact in storage. Of course, the current CPU after Pentium will sacrifice the speed. For example, if your structure members are of the char type, you can:

# Pragma pack (1)
Struct test {
Char;
Char B;
Char C;
...
}

# Pragma pack () // The Structrue defined here will be aligned with the default byte

Answer 02:
# Pragma pack (1)
Typedef struct {
Byte B1;
Int tt;
Byte B2;
} Byte_type;

It is of no major use and is mostly seen in some hooks.Program, Used to modify and runCode.
For example, the value of int nsize = sizeof (byte_typr) defined above is 6. If pack is not used, it is equal to 0x0c. A byte also has a length of 4. After pack is used, it becomes 1.

During code modification, the 1 byte command can only make it a real 1 byte, not a 4 byte. Therefore, it is of little significance in other places. ----- We have mem now. Does not care about a few bytes at all.

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.