__ATTRIBUTE__ ((packed)) usage

Source: Internet
Author: User
Tags compact constant pack


Reprinted from: http://blog.chinaunix.net/uid-25768133-id-3485479.html

Also refer to: http://blog.csdn.net/lmh12506/article/details/25641853

http://blog.csdn.net/wangzhaotongalex/article/details/22729215

Http://stackoverflow.com/questions/8568432/is-gccs-attribute-packed-pragma-pack-unsafe

1. The role of __attribute__ ((packed)) is to tell the compiler to cancel the optimization alignment of the structure during compilation, aligned according to the actual number of bytes consumed, and is a unique syntax for GCC. This feature is not related to the operating system, with the compiler, the GCC compiler is not compact mode, I am under Windows, VC compiler is not compact, with 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 primarily used to set its properties in a function or data declaration. The primary purpose of assigning properties to a function is to have the compiler optimize it. The __attribute__ ((noreturn) in the function declaration tells the compiler that this function is not returned to the caller, so that the compiler can eliminate unnecessary function return code when optimizing.

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

The __ATTRIBUTE__ syntax format is:

__attribute__ ((attribute-list))

Its position constraint: Before the end of the declaration ";".

Function Attribute: 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.

The 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 the variable or struct member use the smallest alignment, that is, the variable is a byte-aligned, and the field (field) is bit-aligned.

If you have seen the implementation of the GPSR protocol in TinyOS, you will certainly notice the following statement:
typedef struct {
Double X;
Double y;
} __attribute__ ((packed)) position_t;

At first we can also understand that it is soon to define a struct. But if you look at the following statements, you may be confused, ' __attribute__ (packed) ' is something. Have any effect. A series of questions will soon pop out of your head. Although this has no effect on understanding the entire program, I do not want these questions to remain in my mind and bear too much weight. Save the future, and perhaps one day can be used on it. Find out the problem.

A major feature of GNU C (but not known to beginners) is the __attribute__ mechanism. __ATTRIBUTE__ can set function properties (functions attribute), variable properties (Variable attribute), and type properties (Type attribute).
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 (type Attribute) that uses 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.

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 my-packed-struct are compact, but the internal member variable S is not "pack", and if you want the internal member variable to be packed, My-unpacked-struct also need to use packed to make 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 is often done by the compiler, and if you are using GCC, you can define variables by adding __attribute__ to decide whether to use memory alignment or memory alignment to a few bytes, as an example of the structure above:
1) to 4 bytes, you can also specify the alignment to 8 bytes.
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
} __attribute__ ((aligned (4)));

2) Not aligned, 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, data structure-based communication (no need to resolve the convection) is used. However, when it is necessary to communicate between multiple platforms, data structure-based communication often has to pay attention to the following aspects:
[1] byte order
[2] variable length
[3] Memory alignment
In common system architectures (Linux x86,windows), non-single-byte-length variable types are low-byte in front, while in some specific systems, such as the Soalris SPARC platform, high-byte is in front. If you do not process before sending the data, then the data value of the Linux X86 to the Soalris SPARC platform will have a great deviation, so that the program does not appear in the process of expected normal results, more serious, will lead to a segment error.
In this case, we tend to use the same byte order. In the system, there are functions such as ntohxxx (), htonxxx (), which are responsible for converting data between the network byte order and the local byte order. Although the local byte order for each system is different, for all systems, the network byte order is fixed-----High byte is in front. Therefore, the network byte order can be 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 code can be easily ported across platforms (like communication, which rarely relies on the system API's code, making a generic version is a good choice).

The length of the variable varies between different systems, as is the Linux2.6.18 platform, where the pointer is 8 bytes in a 64-bit system, and the pointer is 4 bytes long in a 32-bit system---here is just an example where very few people send pointers as data. Here are the lengths of several common C language variables that I've compiled for 64-bit Linux systems and 32-bit Linux systems:
Short int long Long long ptr time_t
32-bit 2 4 4 8 4 4
64-bit 2 4 8 8 8 8
When defining a structure for communication, you should consider using a constant data type, such as a fixed length of uint32_t,4 bytes, and this belongs to the standard C library (C99), which can be used in each system.

The issue of 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, you might want to write a simple program test and you'll see the same struct, even if you use a constant data type, 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 variables occupy space is not aligned to a multiple of 4 or 8, for example, a simple test structure:
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
};
Look at the length of the structure on each system.
Memory alignment is often done by the compiler, and if you are using GCC, you can define variables by adding __attribute__ to decide whether to use memory alignment or memory alignment to a few bytes, as an example of the structure above:
1) to 4 bytes, you can also specify the alignment to 8 bytes.
struct student
{
Char name[7];
uint32_t ID;
Char subject[5];
} __attribute__ ((aligned (4)));

2) Not aligned, 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));

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.