3

Possible Duplicate:
GCC __attribute__((aligned(x)) explanation
What is meaning of the ‘_attribute_((aligned(4)));’ in the first line?

What does these two pieces of code mean? In particular, the __attribute__ ((aligned(..))) parts.

struct my_struct {
  int64_t a;
  int64_t b;
} __attribute__ ((aligned(16)));

and

struct my_struct2 { double arr[4] __attribute__((aligned(64))); };
Community
  • 1
  • 1
JJ Beck
  • 5,193
  • 7
  • 32
  • 36

1 Answers1

5

CPU registers often point to memory.

When you increment a register, it points 64 bits further on a 64 bit machine. However, if you want to get to a byte inside that section, the compiler has to do more work.

If you align memory on the "edges" of memory, the registers can access them much faster. It also means structure members are padded so some space is wasted. If you do a memory dump of the structure, you may be surprised at the padding.

Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • In macOS the compiler align any array to 16 Byte. Does GCC do that as well on 64 Bit system? – Royi Aug 04 '17 at 23:35
  • 1) I have no idea. 2) If you have a new question, you should start a new question and not tag on. 3) Necro posting (posting to very old threads) is frowned upon. – Steve Wellens Aug 05 '17 at 04:04