_ Attribute _ is a mechanism provided by gnu c. _ 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 _, which are followed by a pair of original arc. The corresponding _ attribute _ parameter is in the ARC.
The syntax format of _ attribute _ is:
_ Attribute _ (Attribute-list ))
Its location constraints are:
Placed before 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 is also very easy for non-GNU applications.ProgramTo achieve compatibility.
Gnu cc requires the-wall compiler to enable the function. This is a good way to control warning information. The following describes several common attribute parameters.
_ Attribute _ format
The _ attribute can add features similar to printf or scanf to the declared function. It allows the compiler to check whether the formatting string between the function declaration and the actually called function parameter matches. This function is very useful, especially for handling bugs that are hard to find.
Format syntax format:
Format (archetype, string-index, first-to-check)
The format attribute tells the compiler to check the parameters of the function according to the format rules of the parameter table of printf, scanf, strftime, or strfmon. "Archetype" specifies the style. "string-index" specifies the first parameters of the Input Function to format the string; "First-to-check" specifies the number of parameters of the function to be checked according to the preceding rules.
The specific format is as follows:
_ Attribute _ (format (printf, m, n )))
_ Attribute _ (format (scanf, m, n )))
The meanings of M and N are as follows:
M: The parameters are formatted strings );
N: The first parameter in the parameter set, that is, the parameter "…" The first parameter in the number of function parameters.
_ Attribute _ noreturn
This attribute notifies the compiler function to never return a value. When a function similar to this attribute requires a return value, but cannot run it to the return value, it exits. This attribute can avoid errors. The declaration formats of abort () and exit () in the C library function use this format, as shown below:
Extern void exit (INT) _ attribute _ (noreturn); extern void abort (void) _ attribute _ (noreturn ));
_ Attribute _ const
This attribute can only be used for functions with numeric type parameters. When a function with numeric parameters is repeatedly called, the compiler can optimize the function because the returned values are the same. In addition to the first operation, others only need to return the first result, which can improve the efficiency. This attribute is mainly applicable to functions without static state and side effects, and the return value only depends on the input parameters.
You can also set attributes for variables or structure fields. Here are several common parameter explanations. For more parameters, refer to the connection provided in this Article.
When using the _ attribute _ parameter, you can also add "_" (two underscores) before and after the parameter. For example, use _ aligned _ instead of aligned, in this way, you can use it in the corresponding header file without worrying about whether there is a macro definition with the same name in the header file.
Aligned (alignment)
This attribute specifies the smallest alignment format of a variable or struct Member, in bytes. For example:
Int X _ attribute _ (aligned (16) = 0; the compiler allocates a variable in 16 bytes (note that byte is not bit) alignment. You can also set this attribute for the struct member variable. For example, to create a double-font-aligned int pair, you can write it like this:
Struct Foo {int X [2] _ attribute _ (aligned (8);}; as described above, you can manually specify the aligned format. Similarly, you can also use the default Alignment Method. If aligned is not followed by a specified number, the compiler will use the most useful aligned method based on your target machine. For example:
Short array [3] _ attribute _ (Aligned); select the maximum aligned mode for the target machine to improve the efficiency of the copy operation.
The aligned attribute makes the set object occupy more space. On the contrary, using packed can reduce the space occupied by the object.
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.
Packed
This attribute enables the smallest alignment mode for variables or struct members, that is, one-byte alignment for variables and bit alignment for fields.
You can also set attributes for struct or union. There are roughly six parameter values that can be set: aligned, packed, transparent_union, unused, deprecated, and may_alias.
When using the _ attribute _ parameter, you can also add "_" (two underscores) before and after the parameter. For example, use _ aligned _ instead of aligned, in this way, you can use it in the corresponding header file without worrying about whether there is a macro definition with the same name in the header file.
Aligned (alignment)
This attribute sets an alignment format (in bytes) of the specified size. For example:
Struct s {short f [3];} _ attribute _ (aligned (8 )));
Typedef int more_aligned_int _ attribute _ (aligned (8 )));
This statement forces the compiler to make sure that variables of the type struct s or more-Aligned-int use an 8-byte aligned-int to allocate space.
As mentioned above, you can manually specify the alignment format. Similarly, you can also use the default alignment mode. If aligned is not followed by a specified number, the compiler will use the most useful aligned method based on your target machine. For example:
Struct s {short f [3];} _ attribute _ (Aligned ));
Here, if the size of sizeof (short) is 2 (byte), the size of S is 6. Take the power of 2 so that the value is greater than or equal to 6, then the value is 8, so the compiler sets the alignment of the S type to 8 bytes.
The aligned attribute makes the set object occupy more space. On the contrary, using packed can reduce the space occupied by the object.
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.
Packed
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 ).