Size of struct with a single element

前端 未结 3 1090
半阙折子戏
半阙折子戏 2020-12-11 14:58

Given

struct S {
  SomeType single_element_in_the_struct;
};

Is it always true that

sizeof(struct S) == sizeof(SomeType)
         


        
3条回答
  •  情书的邮戳
    2020-12-11 15:25

    It does not have to be equal, due to structure padding.

    section 6.7.2.1 in the C99 standard states that "There may be unnamed padding within a structure object, but not at its beginning".

    This is refered to as structure padding. Paddings may be added to make sure that the structure is properly aligned in memory. The exakt size of a structure can change if you change the order of its members.

提交回复
热议问题