Detailed English explanation http://www.unixwiz.net/techtips/gnu-c-attributes.html
To write data structures that can be ported between different platforms, you should always force the natural alignment of the data items in addition to the specific byte sequence.
Natural alignment (Natural alignment) is the storage of data items at the address of an integer multiple of the size of the data item (for example, an address of 8-byte data items that are stored as an integer multiple of 8).
To force natural alignment to prevent the compiler from moving the data structure's fields, you should use the fill character (filer) field to avoid leaving a void in the data structures.
The compiler itself may insert fill data into the structure to ensure that alignment of each field can achieve good performance on the target processor.
If you are defining a structure that matches the structure that the device requires, this automatic padding will destroy your intent. The solution is to tell the compiler that the struct body must be "full" and not add a fill character.
is to add __attribute__ ((packed)) to the place where the structure is declared, for example:
struct {
u16 ID;
U64 Lun;
U16 Reserved1;
U32 reserved2;
} __ATTRIBUTE__ ((packed)) SCSI;
If there is no __attribute__ ((packed)), the LUN field is inserted in front of two padding bytes and is aligned by 4 bytes.
1. The role of __attribute__ (packed) is to tell the compiler to cancel the optimization alignment of the structure during compilation, and to align according to the actual byte count, which is GCC-specific syntax. This function is not the same as the operating system, with the compiler, the GCC compiler is not compact mode, I am under Windows, VC compiler is not compact, with the TC compiler is compact. For example:
Under 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 used primarily to set its properties in a function or data declaration. The main purpose of assigning properties to a function is to have the compiler optimize. The __attribute__ ((noreturn)) in the function declaration tells the compiler that the function is not returned to the caller, so that the compiler removes the unnecessary function return code when it is optimized.
One of the big features of GNU C is the __attribute__ mechanism. __ATTRIBUTE__ can set function attributes, variable attributes (Variable attribute), and type attributes.
__attribute__ writing feature is: __attribute__ before and after there are two underscores, and followed by a pair of parentheses, brackets inside the corresponding __attribute__ parameters.
The __ATTRIBUTE__ syntax format is:
__attribute__ ((attribute-list))
Its position constraint: placed before the end of the declaration ";".
Function attributes: Function properties can help developers add features to function declarations, which can make the compiler more powerful in error checking. The __attribute__ mechanism is also easily compatible with non-GNU applications.
GNU cc needs to use the –wall compiler to hit the deserved function, which is a good way to control the warning message.
Packed property: Use this property to make a variable or struct member use the smallest alignment, that is, a byte alignment to a variable, and a bitwise alignment for the field.
If you have seen the implementation of the GPSR protocol in TinyOS, you will surely notice the following statement:
typedef struct {
Double X;
Double y;
} __attribute__ ((packed)) position_t;
At first we can also understand that soon it is to define a structure. But when you see the following statement, you may be confused, ' __attribute__ (packed) ' is something. Have any effect. A series of questions will come out of your head right away. While this has no effect on understanding the whole process, I don't want to keep these questions in my head and put too much weight on them. Save the future, and perhaps one day can be used on it. Find out about the problem.
One of the major features of GNU C (which is not known to beginners) is the __attribute__ mechanism. __ATTRIBUTE__ can set function attributes, variable attributes (Variable attribute), and type attributes.
The __ATTRIBUTE__ syntax format is:
__attribute__ ((attribute-list))
Its position constraint is: placed before the end of the declaration ";".
Packed is a parameter of the type attribute, which can be used to reduce the space occupied by the object packed. It is important to note that the validity of attribute attributes is also related to your connector, and if your connector supports only 16-byte alignment, then it is useless to define 32-byte alignment at this point.
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 is implied 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 My-packed-struct type of variable array are compacted together, but the internal member variable S is not "pack", if the inner member variable is expected to be packed, My-unpacked-struct also need to use packed for the appropriate constraints.
struct MY_UNPACKED_STRUCT
{
char c;
int i;
};
struct MY_PACKED_STRUCT
{
char c;
int i;
struct my_unpacked_struct s;
}__attribute__ ((__packed__));
Look at the length of the structure on each system.
Memory alignment, which is often done by the compiler, if you are using GCC, you can define variables by adding __attribute__ to determine whether to use the memory alignment, or the memory alignment to several bytes, taking the above structure as an example:
1) to 4 bytes, you can also specify alignment to 8 bytes.
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
} __attribute__ ((aligned (4)));
2, the length of the structure, is the length of each variable and
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
} __attribute__ ((packed));
data structure-based network communication across platformsNetwork communication is usually divided into data structure based and stream based. The HTTP protocol is an example of the latter.
Sometimes in order to improve the processing speed of the program and the convenience of data processing, communication based on data structure is used (no convection is required to parse). However, when the need for communication between multiple platforms, based on data structure communication, often have to pay attention to the following aspects:
[1] byte order
[2] variable length
[3] Snap to memory
In a common system architecture (Linux x86,windows), variable types that are not single-byte in length are low byte in front, while in certain systems, such as the Soalris SPARC platform, the high byte is in front. If the data is not processed before it is sent, then the data value from the Linux X86 to the Soalris SPARC platform is bound to have a great deviation, which in turn can not produce the expected normal result during the program running, and will cause a segment error when it is more serious.
In this case, we tend to use the same byte sequence. In the system, there are ntohxxx (), htonxxx () functions, which are responsible for converting the data between the network byte order and the local byte order. Although the local byte order of each system is different, for all systems, the network byte order is fixed-----high byte in front. Therefore, the network byte order can be used as the standard of communication, before sending, the data are converted to network byte order.
Conversion process, it is also recommended to use standard functions such as ntohxxx (), htonxxx (), so that the code can be easily ported across platforms (such as communications, which rarely rely on the system API, to make a generic version is a good choice).
The length of the variable, there will be differences between different systems, like a Linux2.6.18 platform, in a 64-bit system, the length of the pointer is 8 bytes, and in 32-bit systems, the pointer is 4 bytes long---here is just an example where few people send pointers as data. Here are the lengths of several common C-language variables that I've sorted out in 64-bit Linux systems and 32-bit Linux systems:
Short int long Long long ptr time_t
32-digit 2 4 4 8 4 4
64-digit 2 4 8 8 8 8
When defining a structure for communication, you should consider using a constant data type, such as the fixed length of the uint32_t,4 byte, and this is a standard C library (C99) that can be used in each system.
The problem with memory alignment is also related to whether the system is 64-bit or 32-bit. If you have 32-bit and 64-bit systems on hand, write a simple program to test, and you'll see the same structure, even if you use a constant data type, and the size is different in different systems. Alignment is often based on 4 bytes or 8 bytes, as long as you write the test program, the variable space is not aligned to 4 or 8 multiples can be a simple test of the structure of the example bar:
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
};
Look at the length of the structure on each system.
Memory alignment, which is often done by the compiler, if you are using GCC, you can define variables by adding __attribute__ to determine whether to use the memory alignment, or the memory alignment to several bytes, taking the above structure as an example:
1) to 4 bytes, you can also specify alignment to 8 bytes.
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
} __attribute__ ((aligned (4)));
2, the length of the structure, is the length of each variable and
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
} __attribute__ ((packed));