Size of struct with a single element

前端 未结 3 1081
半阙折子戏
半阙折子戏 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:28

    This will usually be the case, but it's not guaranteed.

    Any struct may have unnamed padding bytes at the end of the struct, but these are usually used for alignment purposes, which isn't a concern if you only have a single element.

提交回复
热议问题