difference between #pragma pack (push,1) and #pragma pack (1)

Source: Internet
Author: User

Original link: http://blog.csdn.net/dutysmart/article/details/7098136

This is the parameter setting for the compiler, for the structure byte alignment setting, #pragma pack is the alignment of the specified data in memory.

#pragma pack (n) Action: The C compiler will be aligned by N bytes.
#pragma pack () Effect: cancels custom byte alignment.


#pragma pack (push,1) function: means to set the original alignment of the stack, and set a new alignment to a byte alignment

#pragma pack (POP) Action: Restore Alignment State

As a result, adding push and pop allows alignment to revert to its original state, rather than the compiler default, which can be said to be better, but many times the difference is small

Such as:

#pragma pack (push)//Save alignment status

#pragma pack (4)//set to 4-byte alignment

equivalent to #pragma pack (push,4)

#pragma pack (1) Action: Adjust the boundary alignment of the structure so that it aligns in one byte;< to align the structure in 1-byte alignment >

#pragma pack ()

For example:

#pragma pack (1)

struct sample
{
Char A;
Double b;
};

#pragma pack ()

Note: If you do not enclose #pragma pack (1) and #pragma pack (), sample is aligned by the compiler default (that is, 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), more space-saving, some fields and also make the structure easier to control.

difference between #pragma pack (push,1) and #pragma pack (1)

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.