Structure alignment in GCC (should alignment be specified in typedef?)

Sorry for the stupid question, but if I need to ensure alignment of the structure / class / union, should I add an attribute ((aligned (align))) in typedef declaration?

class myAlignedStruct{} __attribute__ ((aligned(16))); typedef myAlignedStruct myAlignedStruct2; // Will myAlignedStruct2 be aligned by 16 bytes or not? 
+7
source share
2 answers

should I add the ((aligned (align))) attribute to the typedef declaration?

No ... typedefs are simply aliases or aliases for the specified specific type, they do not exist as a separate type to have different alignment, packaging, etc.

 #include <iostream> struct Default_Alignment { char c; }; struct Align16 { char c; } __attribute__ ((aligned(16))); typedef Align16 Also_Align16; int main() { std::cout << __alignof__(Default_Alignment) << '\n'; std::cout << __alignof__(Align16) << '\n'; std::cout << __alignof__(Also_Align16) << '\n'; } 

Output:

 1 16 16 
+8
source

The accepted answer (“No”) is correct, but I wanted to clarify one potentially misleading part of it. I would add a comment, but I need to format the code; hence the new answer.

typedefs are simply aliases or aliases for the specified specific type, they do not exist as a separate type to have different alignment, packaging, etc.

This is not true, at least for GCC (OP compiler) and GHS. For example, the following compilation is error-free, showing that alignment can be attached to a typedef.

Perverted alignment (larger than the size of the object) is simply an impact and entertaining value.

 #define CASSERT( expr ) { typedef char cassert_type[(expr) ? 1 : -1]; } typedef __attribute__((aligned(64))) uint8_t aligned_uint8_t; typedef struct { aligned_uint8_t t; } contains_aligned_char_t; void check_aligned_char_semantics() { CASSERT(__alignof(aligned_uint8_t) == 64); CASSERT(sizeof(aligned_uint8_t) == 1); CASSERT(sizeof(contains_aligned_char_t) == 64); } 
+6
source

All Articles