_ Attribute _ (packed)

Source: Internet
Author: User

When we look at our code, we find that there is a struct type declaration for _ attribute _ (packed) when declaring the type. I don't know what it means, the following figure shows the meaning:

1.The function of _ attribute _ (packed) is to tell the compiler to cancel the optimization alignment of the structure during the compilation process and align it according to the actual number of bytes occupied. It is a special Syntax of GCC. This function has nothing to do with the operating system and is related to the compiler. The GCC compiler is not compact. In Windows, the compiler using VC is not compact, the TC compiler is compact. For example:

In TC: struct my {char ch; int A;} sizeof (INT) = 2; sizeof (my) = 3; (compact Mode)

Under GCC: struct my {char ch; int A;} sizeof (INT) = 4; sizeof (my) = 8; (non-compact Mode)

Under GCC: struct my {char ch; int A ;:__ attrubte _ (packed) sizeof (INT) = 4; sizeof (my) = 5

2.The _ attribute _ keyword is mainly used to set its attributes in functions or data declarations. The main purpose of assigning an attribute to a function is to optimize the compiler. The _ attribute _ (noreturn) in the function Declaration tells the compiler that this function will not be returned to the caller, so that the compiler can remove unnecessary function return code during optimization.

A major feature of gnu c is the _ attribute _ mechanism. _ Attribute _ FUNCTION attribute, variable attribute, and type attribute can be set ).

The _ attribute _ writing feature has two underscores (_) on the front and back of __attribute _, followed by a pair of arc brackets, and the _ attribute _ parameter in the ARC.

The syntax format of _ attribute _ is:

_ Attribute _ (Attribute-list ))

Its location constraint is placed before the ";" at the end of the statement.

Function attribute: Function Attributes can help developers add some features to the function declaration, which makes the compiler more powerful in error checking. The _ attribute _ mechanism can also be easily compatible with non-GNU applications.

Gnu cc requires the-wall compiler to enable the function. This is a good way to control warning information.

Packed attribute: This attribute enables the smallest alignment mode for variables or struct members, that is, one-byte alignment for variables and bit alignment for fields.

 

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.