Syntax format of attribute
attribute ((attribute-list))
Attribute is preceded and followed by two underscores, followed by two pairs of meta-brackets. attribute-list is a comma-separated list of attributes. attribute ((attribute-list)) is placed before the “;” at the end of the declaration.
Common properties
packed
Let the compiler cancel the byte optimization alignment of the structure during compilation and align it according to the actual number of bytes occupied.
- pack can compress the memory space occupied by variables.
- pack acts on the definition of a structure or class.
- The function of pack is to change the layout rules of member variables in a structure or class.
- If the default pack of a certain structure is n and the alignment rule m specified by pack is greater than n, the pack is ignored.
- pack must be the nth power of 2 when specifying rules.
For example, two structures are defined in the source code
|
|
Compiled using clang, the outputs are 4 and 3 respectively when running.
aligned
Specifies the minimum alignment format of variables or structure members, in bytes. Let the user decide how many bytes to align the variable.
|
|
- When aligned is applied to a variable, its function is to tell the compiler that when allocating memory for the variable, it should be allocated in the memory specified for it. Acting on the variable will not change the size of the variable.
A 32-bit variable is defined in the code:
|
|
The memory starting address of the variable var_in_8bytes is a multiple of 8.
- When aligned acts on a type, its role is to tell the compiler that all variables declared in the type must be allocated in memory with the specified alignment. When this attribute is applied to a structure declaration, it may change the size of the structure.
|
|
As shown above, when align is applied to a structure definition, it will change the size of the structure. The final size of the structure is an integer multiple of the size specified by aligned.
section
section controls the section name of a variable or function at compile time. It is widely used in embedded software development. For example, when there is external Flash or RAM, variables or functions need to be placed in the external storage space. You can specify the segment name in the link script to operate. When programming an MCU using MPU (memory protection), it is necessary to divide the memory into areas and place variables or codes into the corresponding areas. This is usually achieved through segment operations.
|
|
After the above code is compiled, the sections where the array and function are located are “indent” and “ext_function” respectively.
unused
It means that the function or variable is likely to be unused, and the compiler will not generate a warning for this function. It can be declared on parameters that are not used in the function implementation, for example:
|
|
used
This attribute is attached to a variable that has static storage, meaning that the variable must be retained even if it does not appear to be referenced. Otherwise, when linking, the linker finds that a variable is not referenced and will optimize the variable.
weak
If two or more global symbols have the same name and one of them is declared as a weak symbol, these global symbols will not cause a redefinition error. When ordinary symbols exist, the linker ignores weak symbols. If ordinary symbols do not exist, weak symbols are used. Functions such as overloading and callbacks can be implemented.