# One side effect of Pragma pack (push, 1)

Source: Internet
Author: User

In C ++, define the communication protocol. h header file. Sometimes # pragma pack (push, 1) is used to modify the memory alignment of Data Structure members (1 byte alignment)

Assume that there is package. h

 

// Filename: package. h

# Pragma pack (push, 1)

Struct performance_test

{

Int iflag1;

Char szname [32];

}

//...

# Pragma pack (POP, 1)

 

If you reference package. h In other C ++ header files, note that the order of package. h affects other referenced header files (actual results under vs2010)

According to the actual measurement, we can conclude that include "package. H" is best placed in the first line of include,

Otherwise, it may affect the heap or stack memory layout of objects defined by other header files, or even cause heap crash or stack upt.

For example:

// Filename: transmit. h

# Pragma once

# Include "otherhead1.h"

# Include "package. H"

# Include "otherhead2.h"

 

//......

 

The above sequence may result in an exception in the data member data of the object defined in otherhead2.h or the function stack is damaged.

The insurance method is to put package. h In the first line of include.

 

======================================

Supplement the truth of the problem. # The Pragma pack (POP, 1) is incorrect,

# Pragma pack (POP) to restore the default memory alignment

 

 

 

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.