问题
int readint(__packed int *data)
{
return *data;
}
I have seen __packed
attribute in struct
declarations to avoid padding. However, what is the benefit of using __packed
attribute in function arguments.
The author says that he has used __packed
to tell the compiler that the integer may possibly not be aligned. What does it means?
Edit: Will the following work with gcc
compiler
int readint(__attribute__((packed)) int *data)
{
return *data;
}
回答1:
The __packed
qualifier is a compiler-specific feature of the armcc
C compiler, published by ARM. A full explanation is present in their documentation, but in brief, it indicates that no padding for alignment should be inserted into the qualified object, and that pointers with this qualifier should be accessed as if they might be misaligned. (This may cause slower code to be generated for some processors, so it should not be used gratuitously.)
Note that this is not the same as the GCC packed
attribute, which only applies to struct
and union
type definitions.
来源:https://stackoverflow.com/questions/28186492/what-is-the-use-of-packed-attribute-in-function-arguments