Why alignment is power of 2?

前端 未结 2 1944
刺人心
刺人心 2021-02-14 11:20

There is a quote from cppreference:

Every object type has the property called alignment requirement, which is an integer value (of type std::size_t, always a

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 12:04

    That's how computers are built.

    A computer has a natural 'word' size that is handled more easily than other sizes. On 64-bit CPUs, the size is 8-bytes. Operating on 8-bytes is most efficient. The hardware is built in a way that fetching memory that is aligned to this word size is also more efficient. So alignment is usually based on the CPU's word size.

    Word sizes are powers of two because, again, that's how computers are built. Everything comes down to bits - so does the number of bits in a word. It's easier to design the hardware where the number of bits in a word is itself a power of two.

提交回复
热议问题