Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

memory

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?

like image 854
Tiago Costa Avatar asked Jan 19 '23 20:01

Tiago Costa


1 Answers

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.

like image 59
Necrolis Avatar answered Jan 30 '23 08:01

Necrolis