__attribute__, how much do you know?

Source: Internet
Author: User
Tags numeric value printf

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 the next one is immediately followed by a pair of original brackets, the corresponding __attribute__ parameters in parentheses.

__ATTRIBUTE__ syntax Format: __attribute__ ((attribute-list))

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

Keyword __attribute__ You can also set property settings for a struct (struct) or a common body (union). Roughly six parameter values can be set, namely: aligned, packed, transparent_union, unused, deprecated and may_alias.

When using the __attribute__ parameter, you can also add "__" (two underscore) before and after the parameter, for example, using __aligned__ instead of aligned, so that you can use it in the corresponding header file without worrying about whether there is a macro definition with duplicate name in the header file.

aligned (alignment)

This property sets a gill (in bytes) of the specified size, for example:

struct S {

Short b[3];

} __attribute__ ((aligned (8)));

typedef int INT32_T __ATTRIBUTE__ ((aligned (8)));

The declaration forces the compiler to ensure that (to the best of its ability) a variable of type struct S or int32_t has a 8-byte alignment when allocating space.

As mentioned above, you can manually specify the alignment format, and you can also use the default alignment. If the aligned is not followed by a specified numeric value, the compiler will use the maximum and most beneficial alignment based on your target machine situation. For example:

struct S {

Short b[3];

} __attribute__ ((aligned));

Here, if the size of sizeof (short) is 2 (byte), then the size of S is 6. Takes a 2-square value that is greater than or equal to 6, the value is 8, so the compiler sets the alignment of type S to 8 bytes.

The aligned property allows the object being set to take up more space, instead, using 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.

packed

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 packed_struct will be tightly bound together, but the internal member variable s will not be "pack", and if you want the internal member variable to be packed, unpacked-struct also needs to use packed to the appropriate constraints.

struct UNPACKED_STRUCT

{

char c;

int i;

};

struct PACKED_STRUCT

{

char c;

int i;

struct unpacked_struct s;

}__attribute__ ((__packed__));

The following example uses the __attribute__ attribute to define a number of structures and their variables, and gives the output and analysis of the results.

The program code is:

1 struct P
 2 
 3 {
 4 
 5 int A;
 6 
 7 char b;
 8 
 9 short C;
Ten 
}__attribute__ ((Aligned (4)) pp;
The 
struct m 
, A; 
int b;
Short 
C; 
}__attribute__ ((Aligned (4)) mm;
( 
a) The struct O, and the 
int A; 
Char b;
Short 
C; 
}oo;
The 
PNS (x 
)
(+ 
) char b;
The 
p px of the same struct;
A short 
C; 
}__attribute__ ((aligned (8)) xx; 
the int main () is 
=%d,sizeof ( 
"sizeof (int)," =%d.sizeof (char) =%d\n " , sizeof (int), sizeof (short), sizeof (char)); 
pp=%d,mm=%d printf ("\ n", sizeof (PP), sizeof (mm)); 
oo=%d,xx=%d printf ("\ n", sizeof (OO), sizeof (XX)); 
return 0; 
63}

Output Result:

sizeof (int) =4,sizeof (short) =2.sizeof (char) =1

Pp=8,mm=12

Oo=8,xx=24

Analysis:

sizeof (PP):

sizeof (a) +sizeof (b) +sizeof (c) =4+1+1=6<8 so sizeof (PP) =8

sizeof (mm):

sizeof (a) +sizeof (b) +sizeof (c) =1+4+2=7

But a after a is populated with 3 bytes, but B is 4 bytes, so a takes 4 bytes, B takes up 4 bytes, and C takes up 4 bytes. So sizeof (mm) =12

sizeof (OO):

sizeof (a) +sizeof (b) +sizeof (c) =4+1+2=7

Because the default is 4-byte alignment, sizeof (OO) =8

sizeof (XX):

sizeof (a) + sizeof (b) =4+1=5

sizeof (PP) = 8; That is, XX is 8-byte aligned, so to add 3 free bytes After a, B, and then to store the PX,

4+1+ (3) +8+1=17

Because XX uses the alignment is 8-byte alignment, so the size of XX must be 8 integer times, that is, XX size is a greater than 17 is a multiple of 8 a minimum value, resulting in

17<24 , so sizeof (XX) =24

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.