C++ Align by structure size or largest alignment requirement among its members?

3

Take this structure for example:

struct Packing
{
     int x; // 4-byte align
     int y; // 4-byte align
     short int z; // 2-byte align
     char m; // 1-byte align;
     char _pad[1]; // explicit padding
};

The sizeof this structure is 12-bytes.

So should store this struct in addresses multiples of the struct size (12-bytes) or in multiples of sizeof(int) (the largest alignment requirement among the members of the struct)?

Since multiples of 12 are also multiples of 4 (sizeof(int)) I guess the struct will be correctly aligned in addresses multiples of 12, but I might waste space that wouldnt be wasted if it was 4-byte aligned.

EDIT: At address 0x00000012 the structure would be aligned and the its first member would also be aligned because 12 is a multiple of 4. What if stored it at address 0x00000004? In this case the first element of the struct would be aligned but what about the structure itself?

c++
memory
asked on Stack Overflow Aug 30, 2011 by Tiago Costa • edited Aug 30, 2011 by Tiago Costa

3 Answers

5

if you want to align for performance on any intel CPU, you should follow these guidelines from the intel optimization manual:

For best performance, align data as follows:

• Align 8-bit data at any address.

• Align 16-bit data to be contained within an aligned 4-byte word.

• Align 32-bit data so that its base address is a multiple of four.

• Align 64-bit data so that its base address is a multiple of eight.

• Align 80-bit data so that its base address is a multiple of sixteen.

• Align 128-bit data so that its base address is a multiple of sixteen.

so in your case, you would align by 16, not 4 or 8, as your struct falls between 64 and 128 bits in length, 16 is the best upper fit, it also enables some other extra stuff, like being able to use SIMD to copy the struct(s) around.

answered on Stack Overflow Aug 30, 2011 by Necrolis • edited Jun 20, 2020 by Community
3

The optimal alignment for a struct is equal to the largest alignment for any of the struct's members. In this case that is 4.

Update

The above assumes that the primary operation you perform on the struct is accessing its members. See the comments to Necrolis's answer for more discussion. In short I suspect that the real answer to your question depends strongly on the hardware involved and the algorithms you are using.

answered on Stack Overflow Aug 30, 2011 by David Heffernan • edited May 23, 2017 by Community
0

The compiler is allowed to leave whatever gaps it wants to ensure that it can access the structure efficiently. Exactly what is needed depends on the underlying architecture. If you are using a 32 bit architecture and there are no half-word or byte data loads, the compiler will probably align all data members (including z, m and _pad) to word boundaries. If however the architecture can do efficient half-word and byte data loads, the you will probably find your struct has the expected sizeof(Packing) == 12

answered on Stack Overflow Aug 30, 2011 by doron

User contributions licensed under CC BY-SA 3.0