Structure alignment-_ packed and # pragma pack

Source: Internet
Author: User
Tags modulus microsoft c

1 ansi c provision


Ansi c specifies that the size of a structure type is the sum of the size of all its fields and the size of the padding areas between or at the end of the field.

The padding area is the space allocated to the struct to make the struct field meet the memory alignment requirements.

 

The structure itself has alignment requirements. The ansi c standard specifies that the alignment requirements of the structure type cannot be looser than the strictest requirements of all its fields.

 

2 Basic alignment rules for Win32 and Linux platforms

 

Many real computer systems have limits on the locations where basic data is stored in the memory. They require that the first address value of the data be K
(Usually 4 or 8), which is called memory alignment, and this K is called alignment modulus of the data type ).

The Microsoft C compiler (cl.exe for 80x86) in win32platform uses the following alignment rules by default:

The alignment modulus of any basic data type T is the size of T, that is, sizeof (t ). For example, for the double type (8 bytes), the address of the Data Type must always be a multiple of 8,

Char data (1 byte) can start from any address.

GCC in Linux follows another set of rules (this is found in the materials and has not been verified. Please correct the error ):
Any 2-byte size (including single-byte ?) The alignment modulus of data types (such as short) is 2, while all other data types that exceed 2 bytes (such as long and double)

All are 4-aligned modulus.

 

3. Alignment processing in vc6

The compilation options in vc6 include/ZP [1 | 2 | 4 | 8 | 16]./zp1 indicates alignment at the boundary of 1 byte, and zpn indicates alignment at the boundary of n Bytes.
The N-byte boundary alignment means that the address of a member must be arranged on an integer multiple address of the member size or an integer multiple address of N, and the minimum values of the two must be obtained.
That is:
Min (sizeof (Member), n)
In fact, the one-byte boundary alignment means that there is no holes between the structure members.

To use this option, you can open the project properties page in vc6, C/C ++ page, select the code generation category, and select in struct member alignment.

 

The/zpn option is applied to the entire project and affects all structures involved in compilation. The default/ZP compact value is/zp8, that is, the default value is 8-byte alignment.

To use alignment options for some schema definitions, use the # pragma pack compilation command.

 

# Pragma pack (push) // save alignment status
# Pragma pack (1) // 1 bytes alignment

Typedef struct
{
Double dvalue1;
Char u8value2;
Int u32value3;
} Asamplestructor;

# Pragma pack (POP) // restore alignment

 

In the preceding example, the size value is 13, indicating that the total length of the structure is 13 bytes after the 1-byte alignment. 16 bytes after alignment is removed.

4 alignment in ARM platform

 

In the ARM platform compiler, there is no parameter alignment instruction such as "# pragma pack". There is only one keyword "_ packed ".

__packedThe qualifier sets the alignment boundary of all valid types to 1. If a structure does not have this qualifier, It is aligned to the data type with the strongest number of tables by default.

 

Typedef _ packed struct
{
Double dvalue1;
Char u8value2;
Int u32value3;
} Asamplestructor;

 

In the preceding example, the size value is 13, indicating that the total length of the structure is 13 bytes after the 1-byte alignment. After the _ packed alignment is removed, it is 16 bytes.

 

Five alignment-related definitions that can run in a VC environment or an arm (Keil) environment at any time

 

We often take the code on the embedded platform to the PC environment for testing. The difference in the environment will make code porting difficult.

The following structure makes porting very easy.

 

# Ifdef Win32

# DEFINE _ packed // in the VC environment, define this keyword as null

 

# Pragma pack (push) // save alignment status
# Pragma pack (1) // 1 bytes alignment

# Endif

 

Typedef _ packed struct
{
Double dvalue1;
Char u8value2;
Int u32value3;
} Asamplestructor;

 

# Ifdef Win32

# Pragma pack (POP) // restore alignment

# Endif

 

In this way, both arm and VC can be compiled, and Code Synchronization will be very simple.

 

However, there is also a simpler method to run the code in the arm (Keil) or VC environment.

# Pragma pack (push, 1)

Typedef struct
{
Double dvalue1;
Char u8value2;
Int u32value3;
} Asamplestructor;

# 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.