Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to place __attribute__ ((aligned)) with typedef:ed struct?

Tags:

gcc

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.

like image 769
P. Petzing Avatar asked Dec 08 '10 15:12

P. Petzing


People also ask

What does __ Attribute__ packed )) Meaning?

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

What is aligned attribute in C?

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.

What is a typedef struct?

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.


1 Answers

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.

like image 162
F'x Avatar answered Sep 19 '22 07:09

F'x