__ATTRIBUTE__ ((packed)) role

Source: Internet
Author: User
Tags compact

1. The role of __attribute__ ((packed)) is to tell the compiler to cancel the optimization alignment of the structure during compilation, aligned according to the actual number of bytes consumed, and is a unique syntax for GCC. This feature is not related to the operating system, with the compiler, the GCC compiler is not compact mode, I am under Windows, VC compiler is not compact, with TC compiler is compact. For example:

Under 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 primarily used to set its properties in a function or data declaration. The primary purpose of assigning properties to a function is to have the compiler optimize it. The __attribute__ ((noreturn) in the function declaration tells the compiler that this function is not returned to the caller, so that the compiler can eliminate unnecessary function return code when optimizing.

A major feature of GNU C is the __attribute__ mechanism. __ATTRIBUTE__ can set function properties (functions attribute), variable properties (Variable attribute), and type properties (Type attribute).

__attribute__ writing features are: There are two underscores before and after the __attribute__, and immediately followed by a pair of parentheses, the corresponding __attribute__ parameters in parentheses.

The __ATTRIBUTE__ syntax format is:

__attribute__ ((attribute-list))

Its position constraint: Before the end of the declaration ";".

Function Attribute: Function properties can help developers add features to function declarations, which can make the compiler more powerful in error checking. The __attribute__ mechanism is also easily compatible with non-GNU applications.

The GNU cc needs to use the –wall compiler to hit the deserved function, which is a good way to control the warning message.

Packed property: Use this property to make the variable or struct member use the smallest alignment, that is, the variable is a byte-aligned, and the field (field) is bit-aligned.

If you have seen the implementation of the GPSR protocol in TinyOS, you will certainly notice the following statement:

    1. typedef struct {
    2. Double X;
    3. Double y;
    4. } __attribute__ ((packed)) position_t;

At first we can also understand that it is soon to define a structure! But if you see the following statement, you may be confused, ' __attribute__ (packed) ' What is it? What's the effect? A series of questions will soon pop out of your head. Although this has no effect on understanding the entire program, I do not want these questions to remain in my mind and bear too much weight. Save the future, and perhaps one day can be used on it. Find out the problem!

A major feature of GNU C (but not known to beginners) is the __attribute__ mechanism. __ATTRIBUTE__ can set function properties (functions attribute), variable properties (Variable attribute), and type properties (Type attribute).
The __ATTRIBUTE__ syntax format is:
__attribute__ ((attribute-list))

Its position constraint is placed before the end of the declaration ";".

Packed is a parameter of the type attribute (type Attribute) that uses packed to reduce the space occupied by the object. It is important to note that the effect of the attribute attribute is also relevant to your connector, and if your connector supports only 16-byte alignment, then it is useless to define 32-byte alignment at this point.

Use this property to define a struct or union type to set the memory constraints for each variable of its type. When used in the enum type definition, it implies that the smallest complete type should be used (it indicates that the smallest integral type should is used).

In the following example, the values in the variable array of type my-packed-struct are compact, but the internal member variable S is not "pack", and if you want the internal member variable to be packed, My-unpacked-struct also need to use packed to make the appropriate constraints.

      1. struct MY_UNPACKED_STRUCT
      2. {
      3. char c;
      4. int i;
      5. };
      6. struct MY_PACKED_STRUCT
      7. {
      8. char c;
      9. int i;
      10. struct my_unpacked_struct s;
      11. }__attribute__ ((__packed__));

__ATTRIBUTE__ ((packed)) role

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.