calculating the size of structure

前端 未结 3 1131
南方客
南方客 2021-01-26 10:09

I was going through the concept of structure member alignment and structure padding concept when I found that the size of this struct ...

struct nod         


        
3条回答
  •  花落未央
    2021-01-26 11:00

    The padding and alignment of members of a struct are completely at the discretion of the compiler, except that there cannot be any padding before the first member. With that said, compilers normally choose padding to serve alignment purposes, so that, in particular,

    1. it is possible for all the members of the same struct to be optimally aligned at the same time, and
    2. in an array of structs, if the members of the first array element are aligned optimally then so will be the members of all the others.

    Subject to those and similar requirements, compilers typically choose the least amount of padding necessary (ideally no padding at all). If different member types have different alignment requirements, then the above considerations require struct representations to be padded to a multiple of the size of the member with the most stringent alignment requirement. They may also require padding between members, depending on the types of the members and their order.

提交回复
热议问题