GCC _ attribute _ details

Source: Internet
Author: User

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 is required
-The wall compiler can attack the deserve function, which is a good way to control the 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.

If you have read the implementation of gpsr in tinyos, you will surely notice the following statement:
Typedef struct {

Double X;

Double Y;
} _ Attribute _ (packed) position_t;


At the beginning, we can still understand that we will soon define a struct! However, if you see the following statement, you may be confused ,'
_ Attribute _ (packed) 'is something? What is the function? A series of questions will come out of your head immediately. Although this has no impact on understanding the entire program, I don't want to keep these questions in my mind, so the burden is too heavy. It is easy to remember, and it may be useful one day. Find out the problem!

GNU
A major feature of C (but not known to beginners) is the _ attribute _ mechanism. _ Attribute _ you can set function attributes (Function
Attribute), variable attribute (Variable
Attribute and type
Attribute ).

The syntax format of _ attribute _ is:

_ Attribute _ (Attribute-list ))


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


Packed is a type property (Type
Attribute). You can use packed to reduce the space occupied by objects. It should be noted that the effectiveness of attribute attributes is also related to your connector. If your connector supports 16-byte alignment at most, defining 32-byte alignment at this time will not help.


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


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

Struct my_unpacked_struct
{

Char C;

Int I;
};

Struct my_packed_struct
{

Char C;

Int I;

Struct my_unpacked_struct S;
}__ Attribute _ (_ packed __));

 

 

Check the length of this struct in each system.

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. The above struct is used as an example:

1) to 4 bytes, you can also specify the alignment to 8 bytes.
Struct student
{
Char
Name [7];
Uint32_t
ID;
Char
Subject [5];
} _ Attribute __
(Aligned (4 )));

2) Non-Alignment. The length of the struct is the sum of the length of each variable.
Struct student
{
Char
Name [7];
Uint32_t
ID;
Char
Subject [5];
} _ Attribute _ (packed ));

 

[Transfer] http://blog.sina.com.cn/s/blog_7e719f0501012tkt.html

 

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.