______________在多个变量的定义中 [英] __attribute__ in definitions of multiple variables
问题描述
我有一个最好的例子可以解释的问题.请考虑以下代码:
I have a question which is best explained by example. Please consider the following code:
unsigned char a,
b;
这显然定义了两个类型为unsigned char
的变量.
This obviously defines two variables of type unsigned char
.
如果我想使变量与16字节边界对齐,那么我的第一个幼稚方法就是:
If I would like to make the variables aligned to 16-byte-boundaries, my first naive approach would be this:
__attribute__((aligned(16))) unsigned char a,
b;
我的问题是我不确定编译器是否始终将__attribute__((aligned(16)))
应用于两个变量.
My problem is that I am not sure whether the compiler always applies __attribute__((aligned(16)))
to both variables.
我特别担心,因为以下所有代码的编译均没有错误或警告:
I am particularly worried because all of the following code is compiled without errors or warnings:
unsigned char a __attribute__((aligned(16)));
unsigned char __attribute__((aligned(16))) b;
__attribute__((aligned(16))) unsigned char c;
根据我的研究,__attribute__((aligned(16)))
对上面三行中的各个变量都做相同的操作.但是,如此弱的语法对于C语言来说并不常见,所以我有点不信任.
According to my research, __attribute__((aligned(16)))
does the same to the respective variable in the three lines above. But such a weak syntax would be unusual for C, so I am somehow mistrustful.
回到我原来的问题,我知道我可以通过类似的方法轻松避免不确定性
Returning to my original problem, I am aware that I easily could avoid the uncertainty by something like
__attribute__((aligned(16))) unsigned char a;
__attribute__((aligned(16))) unsigned char b;
或者也许
unsigned char a __attribute__((aligned(16))),
b __attribute__((aligned(16)));
但是我真的很想知道在声明多个 all 应该具有属性的变量时,添加__attribute__
装饰一次是否足够.
But I really would like to know whether it is sufficient to add the __attribute__
decoration once when declaring multiple variables which all should have the attribute.
当然,该问题涉及所有属性(不仅是aligned
属性).
Of course, that question relates to all attributes (not only the aligned
attribute).
作为一个额外的问题,将这样的属性不仅添加到变量定义中,而且还添加到变量声明中(例如,头文件中),是否被认为是一种好风格?
As a bonus question, is it considered good style to add such attributes not only to the variable definitions, but also to the variable declarations (e.g. in header files)?
推荐答案
是;两者
__attribute__((aligned(16))) unsigned char a, b;
和
unsigned char __attribute__((aligned(16))) a, b;
将a
和b
对齐到16个字节的边界. gcc将__attribute__
作为类型的一部分(例如const
和volatile
修饰符)进行处理,以便将诸如
align a
and b
to 16 byte boundary. gcc handles __attribute__
as part of the type (like const
and volatile
modifiers) so that mixed things like
char * __attribute__((__aligned__(16))) * a;
也是可能的.
https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html#Attribute-Syntax 说:
属性说明符列表可能紧接在逗号,=或分号之前,终止了除函数定义以外的标识符声明.此类属性说明符适用于已声明的对象或函数
An attribute specifier list may appear immediately before the comma, = or semicolon terminating the declaration of an identifier other than a function definition. Such attribute specifiers apply to the declared object or function
这就是为什么
unsigned char a __attribute__((aligned(16))), b;
仅适用于a
,而不适用于b
.
would apply to a
only but not to b
.
在另一种情况下
unsigned char a, __attribute__((aligned(16))) b;
仅b
对齐.在这里
在以逗号分隔的声明符列表中,属性说明符列表可能紧接在声明符(而不是第一个)之前出现...此类属性说明符仅适用于在它们出现在其声明符之前的标识符
An attribute specifier list may appear immediately before a declarator (other than the first) in a comma-separated list of declarators ... Such attribute specifiers apply only to the identifier before whose declarator they appear
来自 https://stackoverflow.com/a/31067623/5639126 .
为避免所有歧义,最好创建一个新类型并使用它.例如
To avoid all the ambiguities, it would be better to create a new type and use this. E.g.
typedef char __attribute__((__aligned__(16))) char_aligned_t;
char_alignedt d, d1;
通过此示例和您的
unsigned char a __attribute__((aligned(16))), a1;
unsigned char __attribute__((aligned(16))) b, b1;
__attribute__((aligned(16))) unsigned char c, c1;
gcc创建(gcc -c
),并且readelf
显示所描述的对齐方式
gcc creates (gcc -c
) and readelf
shows the described alignments
8: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM a
9: 0000000000000001 1 OBJECT GLOBAL DEFAULT COM a1 <<< not aligned!
10: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM b
11: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM b1
12: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM c
13: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM c1
14: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM d
15: 0000000000000010 1 OBJECT GLOBAL DEFAULT COM d1
这篇关于______________在多个变量的定义中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!