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

前端 未结 4 538
执笔经年
执笔经年 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:06

    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.

提交回复
热议问题