What is the use of __packed attribute in function arguments

。_饼干妹妹 提交于 2020-07-07 07:15:28

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!