__attribute__ ((packed)) byte alignment

1. __attribute__ ((packed)) role is to tell the compiler to cancel the structural optimization during compilation alignment, aligned in accordance with the actual number of bytes occupied, are GCC-specific syntax. This feature is okay with the operating system, with the compiler about, gcc compiler is not compact model, I'm under windows, using vc compiler is not compact, with tc compiler is compact. For example:
In the TC: struct my {char ch; int a;} sizeof (int) = 2; sizeof (my) = 3; ( compact mode)
at GCC: struct my {char ch; int a;} sizeof ( int) = 4; sizeof (my ) = 8; ( non-compact mode)
at GCC: struct my {char ch; int a;} __ attrubte__ ((packed)) sizeof (int) = 4; sizeof (my) = 5
2. __attribute__ keyword is used mainly to their properties in a function or a data declaration. The main purpose of the function is assigned to the property to allow the compiler to optimize. __Attribute __ function declaration ((noreturn)), tells the compiler that this function does not return to the caller, so that the compiler when optimizing the return code to remove unnecessary functions.
A major feature of GNU C is __attribute__ mechanism. __attribute__ function attribute may be provided (Function Attribute), variable attributes (Variable Attribute) and type attributes (Type Attribute).


Writing __attribute__ wherein: __ attribute__ two underscores before and after, and immediately followed by one pair of brackets, which brackets are the corresponding parameters __attribute__.


__attribute__ syntax is:


__attribute__ ((attribute-list))


Its location constraints: put in the declaration of the tail ";" before.


Function Properties (Function Attribute): Function properties can help developers to add features to the function declaration so that the compiler can function in terms of a more powerful error checking. __attribute__ mechanism is also very easy to do with the effect of non-compliant GNU applications.


GNU CC need to use -Wall compiler to strike deserve function, which is a good way to control warning messages.


packed attribute: This attribute can be used such that the variable or structure members with a minimum of alignment, i.e. is a byte aligned variable, domain (field) are bit aligned.
If you've seen GPSR protocol in TinyOS in, you will notice the following statement:
typedef struct {
    Double the X-;
    Double the y-;
} __attribute __ ((packed The)) position_t;
start we can also understand that soon is the definition of a structure body Well! But to see the statements that follow, you will probably confused the, '__attribute __ ((packed) )' What is this? what's the effect? A series of questions will soon emerge from your head. Although this has no effect on the understanding of the whole process, but I do not want these questions have been stay in my mind, the burden is too heavy. Save later obsession, and perhaps one day be used on it. Understand this problem now!
A major feature of GNU C (not known to be a beginner) is __attribute__ mechanism. __attribute__ function attribute may be provided (Function Attribute), variable attributes (Variable Attribute) and type attributes (Type Attribute).
__attribute__ syntax is:
__attribute__ ((attribute-List))
its position constraint is: put the tail declaration ";" before.
It is a packed type attribute (Type Attribute) of a parameter, using the packed objects can reduce the space occupied. It should be noted that the effectiveness of attribute properties and also your connection, and if your connector only supports a maximum 16-byte aligned, then you define the 32-byte aligned at this time is useless.
Using this attribute defined struct types or a union, each variable setting memory constraints of its type. When used in enum type definition, it implies type (it indicates that the smallest integral type should be used) should use the smallest complete.
In the following example, the value of variable array my-packed-struct types will compact together, but the internal member variable s is not "pack", if you want the internal member variable is also packed words, my-unpacked- also requires the use of packed struct corresponding constraints.
my_unpacked_struct struct
{
     char C;
     int I;
};
         
struct my_packed_struct
{
     char C;
     int I;
     struct my_unpacked_struct S;
} __ attribute__ ((__packed__));
 
 
each system point of view the length of this structure it.
    Memory alignment is often done by the compiler, if you are using gcc, the definition of variables can be added to the __attribute__, to decide whether to use the memory alignment, or a few bytes to the memory alignment to the above structure an Example:
 1) to 4 bytes can be specified similarly to the 8-byte aligned.
Student struct
{
    char name [. 7];
    uint32_t ID;
    char subject[5];
} __attribute__ ((aligned(4))); 


2) are not aligned, the body length of the structure, and that the length of each variable
struct Student
{
    char name [. 7];
    uint32_t ID;
    char Subject [. 5];
} the __attribute__ ((packed The));
-------- -------------

Transfer: https: //blog.csdn.net/turkeyzhou/article/details/8560545

Guess you like

Origin www.cnblogs.com/noxy/p/11235793.html