memory allocation for structures elements

前端 未结 7 1292
故里飘歌
故里飘歌 2021-01-19 05:14

Hi I am having difficulties in understanding about how the memory is allocated to the structure elements.

For example if i have the below structure and the size of c

7条回答
  •  萌比男神i
    2021-01-19 05:51

    In your calculation, you ignore the fact that e is subject to be padded as well:

    The struct looks like

    0       8       16      24      32
    AAAAaaaaBBBBBBBBCcccccccDDDDDDDDEEEEeeee
    

    where uppercase is the variable itself, and lowercase is the padding applied to it.

    As you see (and as well from the addresses), each field is padded to 8 bytes, which is the largest field in the structure.

    As the structure might be used in an array, and all array elements should be well-aligned as well, the padding to e is necessary.

提交回复
热议问题