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.