I have a question which is best explained by example. Please consider the following code:
unsigned char a,
b;
This obviously defines two variables of type unsigned char
.
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;
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;
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;
or perhaps
unsigned char a __attribute__((aligned(16))),
b __attribute__((aligned(16)));
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.
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)?