不同编译器的枚举大小(以字节为单位)
枚举的大小在不同编译器(gcc、Visual C 等?)中始终相同。也就是说,特定枚举的 sizeof() 是否为每个遵循 C/C++ 标准的编译器提供相同的值?
is the size of an enum always the same among different compilers (gcc, visual c and others?). That is, does sizeof() of a specific enum gives the same values with every compiler that follow the C/C++ standards?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不会。
在 C 和 C++ 中,枚举的大小使得所有值都可以表示并与整数类型兼容。不同的编译器可能使用不同的算法来选择类型(如果没有由另一个标准指定,例如明确定义的 ABI)。 (C++11 允许使用新语法指定底层类型)
No.
In both C and C++ an enum will have a size such that all the values can be represented and be compatible with an integer type. Different compilers may use different algorithm to choose the type (if it is not specified by another standard such a clearly defined ABI). (C++11 allows to specify the underlying type with a new syntax)
ISO/IEC 9899:1999 (E) p.105
所以我们只有 sizeof(enum) 的上限。在大多数系统上,我有 sizeof(enum) = 4,但 STM 编译器使 sizeof (enum) = 1/2/4 取决于枚举中写入的值
编辑:似乎您可以将枚举的值之一设置为 max int 以确保编译器选择整数作为枚举 尺寸。
ISO/IEC 9899:1999 (E) p.105
So we only have upper boundaries for sizeof(enum). On most systems i had sizeof(enum) = 4, but STM compiler made sizeof(enum) = 1/2/4 depending on values written in enum
Edit: it seems that you can set one of your enum's value to max int to ensure that compiler chooses integer as enum size.