Is the size of a struct required to be an exact multiple of the alignment of that struct?

前端 未结 9 2066
萌比男神i
萌比男神i 2020-11-28 13:00

Once again, I\'m questioning a longstanding belief.

Until today, I believed that the alignment of the following struct would normally be 4 and the size would normall

9条回答
  •  难免孤独
    2020-11-28 13:16

    One definition of alignment size:

    The alignment size of a struct is the offset from one element to the next element when you have an array of that struct.

    By its nature, if you have an array of a struct with two elements, then both need to have aligned members, so that means that yes, the size has to be a multiple of the alignment. (I'm not sure if any standard explicitly enforce this, but because the size and alignment of a struct don't depend on whether the struct is alone or inside an array, the same rules apply to both, so it can't really be any other way.)

提交回复
热议问题