I have Googled information on gcc's __attribute__ ((aligned))
to learn more about how to use the attribute.
According to GNU "You may specify aligned and transparent_union attributes either in a typedef declaration or just past the closing curly brace of a complete enum, struct or union type definition and the packed attribute only past the closing brace of a definition." In addition the document shows the following example:
struct S { short f[3]; } __attribute__ ((aligned (8)));
But I have found few examples with "typedef struct". I found the following two:
typedef struct __attribute__ ((aligned)) { char a; int x; } foo;
typedef struct { char a; int x; } __attribute__ ((aligned)) foo;
Which is the preferred method: attribute after struct and before {
, or attribute after }
and before foo?
Do they both deliver the same result?
I would greatly appreciate any additional detail about the correct usage of __attribute__ ((aligned))
with a typedef:ed struct.
__attribute__((packed)) variable attributeThe attribute specifies that a member field has the smallest possible alignment. That is, one byte for a variable field, and one bit for a bitfield, unless you specify a larger value with the aligned attribute.
The aligned variable attribute specifies a minimum alignment for the variable or structure field, measured in bytes. The aligned attribute only increases the alignment for a struct or struct member. For a variable that is not in a structure, the minimum alignment is the natural alignment of the variable type.
typedef is a predefined keyword. You can replace the name of the existing data type with the name which you have provided. This keyword helps in creating a user-defined name for an existing data type. You can also use the typedef keyword with structures, pointers, arrays etc.
From the GCC doc:
For an enum, struct or union type, you may specify attributes either between the enum, struct or union tag and the name of the type, or just past the closing curly brace of the definition. The former syntax is preferred.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With