Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does size of the struct need to be a multiple of the largest alignment of any struct member

I understand the padding that takes place between the members of a struct to ensure correct alignment of individual types. However, why does the data structure have to be a multiple of alignment of largest member? I don't understand the padding is needed at the end.

Reference: http://en.wikipedia.org/wiki/Data_structure_alignment

like image 766
user236215 Avatar asked Apr 25 '12 03:04

user236215


2 Answers

Good question. Consider this hypothetical type:

struct A {
    int n;
    bool flag;
};

So, an object of type A should take five bytes (four for the int plus one for the bool), but in fact it takes eight. Why?

The answer is seen if you use the type like this:

const size_t N = 100;
A a[N];

If each A were only five bytes, then a[0] would align but a[1], a[2] and most of the other elements would not.

But why does alignment even matter? There are several reasons, all hardware-related. One reason is that recently/frequently used memory is cached in cache lines on the CPU silicon for rapid access. An aligned object smaller than a cache line always fits in a single line (but see the interesting comments appended below), but an unaligned object may straddle two lines, wasting cache.

There are actually even more fundamental hardware reasons, having to do with the way byte-addressable data is transferred down a 32- or 64-bit data bus, quite apart from cache lines. Not only will misalignment clog the bus with extra fetches (due as before to straddling), but it will also force registers to shift bytes as they come in. Even worse, misalignment tends to confuse optimization logic (at least, Intel's optimization manual says that it does, though I have no personal knowledge of this last point). So, misalignment is very bad from a performance standpoint.

It usually is worth it to waste the padding bytes for these reasons.

Update: The comments below are all useful. I recommend them.

like image 77
thb Avatar answered Oct 04 '22 03:10

thb


Depending on the hardware, alignment might be necessary or just help speeding up execution.

There is a certain number of processors (ARM I believe) in which an unaligned access leads to a hardware exception. Plain and simple.

Even though typical x86 processors are more lenient, there is still a penalty in accessing unaligned fundamental types, as the processor has to do more work to bring the bits into the register before being able to operate on it. Compilers usually offer specific attributes/pragmas when packing is desirable nonetheless.

like image 21
Matthieu M. Avatar answered Oct 04 '22 02:10

Matthieu M.