size of struct and corresponding variable

前端 未结 6 1081
慢半拍i
慢半拍i 2021-01-14 07:53

If i define a char variable

char a;

and a structure with a single char member

struct OneChar {
char a;
};
<
6条回答
  •  逝去的感伤
    2021-01-14 08:14

    Various compilers will optimize to add padding between or at the end of the struct. So, it's not safe on all compilers or platforms to assume the allocated size is what it seems. Check your compiler options for setting struct padding.

    For instance, Visual Studio uses the #pragma pack directive to override default optimizations.

提交回复
热议问题