_ Attribute _ (packed)

Source: Internet
Author: User

1.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 responses during optimization.Code.

A major feature of gnu c is the _ attribute _ mechanism. _ Attribute _ YesSetFunction attribute, variable attribute, and type attribute ).

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: The function attribute can beHelpDevelopmentAdd some features to the function declaration to make the compiler more powerful in error checking. The _ attribute _ mechanism is also easy to be the same as the GNUApplicationProgramTo achieve compatibility.

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

2. Packed attribute: packed is a parameter of Type attribute. Using packed can reduce the space occupied by objects. This attribute enables the smallest alignment mode for variables or struct members, that is, one-byte alignment for variables and bit alignment for fields.

 

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 is irrelevant to the operating system and related to the compiler,The GCC compiler is not compact. In Windows, the VC compiler is not compact, and 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

 

Use this attributeStructOrUnionDefines the type and sets the memory constraints for each variable of its type. When usedEnumWhen defining a type, it implies that the minimum complete type should be used (It indicates that the smallest integral type shoshould be used).

In the following example,My-packed-structThe values in the array of variable types are compact, but the internal member variablesSWill not be"Pack"If you want internal member variables to bePackedThen,My-unpacked-structYou also need to usePacked.
Struct my_unpacked_struct
{
Char C;
Int I;
};

Struct my_packed_struct
{
Char C;
Int I;
Struct my_unpacked_struct S;
}__ Attribute _ (_ packed __));

 

 

In each system, let's look at the length of the two struct below.
Memory alignment is often done by the compiler. If you are using gcc, you can add _ attribute __when defining variables to determine whether to use memory alignment, or the memory is aligned to several Bytes:

 

# Include <stdio. h> //To 4 bytes. You can also specify the alignment to 8 bytes.  Struct  Student1 {  Char Name [ 7  ]; Unsigned  Int  ID;  Char Subject [ 5  ];} _ Attribute _ (aligned (  4  )));//Non-Alignment. The length of the struct is the sum of the variable lengths.  Struct  Student2 { Char Name [ 7  ]; Unsigned  Int  ID;  Char Subject [ 5  ];} _ Attribute _ (packed ));  Int  Main () {printf (  "  % D \ n  " , Sizeof ( Struct Student1); // 20 printf (  "  % D \ n  " , Sizeof ( Struct  Student2); // 16} 

 

 

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.