Can C arrays contain padding in between elements?

后端 未结 5 740
长情又很酷
长情又很酷 2020-11-27 19:55

I heard a rumor that, in C, arrays that are contained inside structs may have padding added in between elements of the array. Now obviously, the amount of padding could not

5条回答
  •  Happy的楠姐
    2020-11-27 20:39

    Careful here. Padding may be added at the end of the struct, but will not be added between the elements of the array as you state in your question. Arrays will always reference contiguous memory, though an array of structures may have padding added to each element as part of the struct itself.

    In your example, the values and foo.values arrays will have the same size. Any padding will be part of the struct foo instead.

提交回复
热议问题