8

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

class myAlignedStruct{} __attribute__ ((aligned(16)));
typedef myAlignedStruct myAlignedStruct2; // Will myAlignedStruct2 be aligned by 16 bytes or not?
Marcus Borkenhagen
  • 6,536
  • 1
  • 30
  • 33
Anonymous
  • 105
  • 2
  • 4
  • 1
    Definitely not a stupid question. I think myAlignedStruct2 is aligned the same way as myAlignedStruct but would like to be sure of it. Did you try a printf("sizes: %d, %d", sizeof(myAlignedStruct), sizeof(myAlignedStruct2)); ? – Shlublu Jul 22 '11 at 06:24
  • @Shlublu: `sizeof` checks packing, but alignment is different! There's no standard operator, but GCC provides `__alignof__()` as illustrated in my answer. – Tony Delroy Jul 22 '11 at 06:48
  • Ah, sorry, i've mixed up! This makes your question even more interesting! – Shlublu Jul 22 '11 at 07:31

2 Answers2

10

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

No... typedefs are just pseudonyms or aliases for the actual type specified, they don't exist as a separate type to have different alignment, packing 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
Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
  • A little confused, Do you mean yes, myAlignedStruct2 be aligned by 16 bytes? – Alok Save Jul 22 '11 at 06:21
  • @Als: I mean "should I add attribute((aligned(align))) to typedef declaration?": no. Yes, "myAlignedStruct2 [will] be aligned by 16 bytes". Illustrative code & output added above.... – Tony Delroy Jul 22 '11 at 06:46
  • My +1. That's correct.There were two Q's & the explicit mention of Yes & No for each makes it very clear now. :) – Alok Save Jul 22 '11 at 06:57
  • @Als: I'm with you now - I hadn't really read the code comments... 8-), only noticed the one question... sorry! – Tony Delroy Jul 22 '11 at 07:26
6

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

typedefs are just pseudonyms or aliases for the actual type specified, they don't exist as a separate type to have different alignment, packing etc..

This is incorrect, at least for GCC (the OP's compiler), and GHS. For example, the following compiles without errors, showing that the alignment can be attached to a typedef.

The perverse alignment (greater than the size of the object) is merely for shock and amusement 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);
}
Paul Du Bois
  • 2,097
  • 1
  • 20
  • 31