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

前端 未结 4 570
执笔经年
执笔经年 2021-01-04 11:23

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

4条回答
  •  庸人自扰
    2021-01-04 12:01

    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.

提交回复
热议问题