函数参数中__packed属性的用途是什么 [英] What is the use of __packed attribute in function arguments

查看:325
本文介绍了函数参数中__packed属性的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int readint(__packed int *data)
{
    return *data;
}

我已经在struct声明中看到了__packed属性,以避免填充.但是,在函数参数中使用__packed属性有什么好处.

I have seen __packed attribute in struct declarations to avoid padding. However, what is the benefit of using __packed attribute in function arguments.

作者表示他已经使用过__packed告诉编译器该整数可能未对齐.是什么意思?

The author says that he has used __packed to tell the compiler that the integer may possibly not be aligned. What does it means?

编辑:以下内容是否可以与gcc编译器一起使用

Will the following work with gcc compiler

int readint(__attribute__((packed)) int *data)
{
    return *data;
}

推荐答案

__packed限定符是armcc C编译器的特定于编译器的功能,由ARM发布. 他们的文档中有完整的解释,但总而言之,它指示没有将用于对齐的填充插入到合格的对象中,并且应访问带有此限定符的指针,就像它们可能未对齐一样. (这可能会导致某些处理器生成的代码变慢,因此不应随意使用它.)

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.)

请注意,这与GCC packed属性不同,后者仅适用于structunion类型定义.

Note that this is not the same as the GCC packed attribute, which only applies to struct and union type definitions.

这篇关于函数参数中__packed属性的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆