问题描述
sizeof(enum) == sizeof(int),总是吗?
Is the sizeof(enum) == sizeof(int), always ?
- 还是依赖于编译器?
- 这样说是不是错了,因为编译器针对字长(内存对齐)进行了优化,即 y int 是特定编译器上的字长?如果我使用枚举,这是否意味着没有处理惩罚,因为它们会字对齐?
- 如果我把所有的返回码放在一个枚举中不是更好吗,因为我显然不担心它得到的值,在检查返回类型时只关心名称.如果是这种情况,#DEFINE 不会更好,因为它可以节省内存.
通常的做法是什么?如果我必须通过网络传输这些返回类型,并且必须在另一端完成一些处理,那么您更喜欢 enums/#defines/const ints.
What is the usual practice?If I have to transport these return types over a network and some processing has to be done at the other end, what would you prefer enums/#defines/ const ints.
编辑 - 只是检查网络,因为编译器不符号链接宏,人们如何调试然后将整数值与头文件进行比较?
EDIT - Just checking on net, as complier don't symbolically link macros, how do people debug then, compare the integer value with the header file?
来自答案——我在下面添加这一行,因为我需要澄清——
From Answers —I am adding this line below, as I need clarifications—
"所以它是实现定义的,并且sizeof(enum) 可能 等于sizeof(char), 即 1."
- 是不是说编译器会检查枚举中的取值范围,然后分配内存.我不这么认为,我当然不知道.有人可以解释一下什么是可能".
推荐答案
它依赖于编译器并且可能因枚举而异.以下是语义
It is compiler dependent and may differ between enums. The following are the semantics
enum X { A, B };
// A has type int
assert(sizeof(A) == sizeof(int));
// some integer type. Maybe even int. This is
// implementation defined.
assert(sizeof(enum X) == sizeof(some_integer_type));
注意一些整数类型"在 C99 中还可能包括扩展的整数类型(但是,如果提供它们,则实现必须记录).枚举的类型是某种可以存储任何枚举器(在本例中为A
和B
)的值的类型.
Note that "some integer type" in C99 may also include extended integer types (which the implementation, however, has to document, if it provides them). The type of the enumeration is some type that can store the value of any enumerator (A
and B
in this case).
我认为使用枚举没有任何惩罚.枚举数也是整数常量表达式(例如,您可以使用它来初始化静态或文件范围变量),并且我尽可能喜欢它们而不是宏.
I don't think there are any penalties in using enumerations. Enumerators are integral constant expressions too (so you may use it to initialize static or file scope variables, for example), and i prefer them to macros whenever possible.
枚举器不需要任何运行时内存.只有在创建枚举类型的变量时,才可以使用运行时内存.只需将枚举器视为编译时常量即可.
Enumerators don't need any runtime memory. Only when you create a variable of the enumeration type, you may use runtime memory. Just think of enumerators as compile time constants.
我只会使用一种可以存储枚举器值的类型(我应该事先知道值的粗略范围),转换为它,然后通过网络发送它.最好是一些固定宽度的类型,比如int32_t
,这样当涉及不同的机器时它不会发生冲突.或者我会打印号码,然后在另一面扫描,这样可以解决其中的一些问题.
I would just use a type that can store the enumerator values (i should know the rough range of values before-hand), cast to it, and send it over the network. Preferably the type should be some fixed-width one, like int32_t
, so it doesn't come to conflicts when different machines are involved. Or i would print the number, and scan it on the other side, which gets rid of some of these problems.
回复编辑
好吧,编译器不需要使用任何大小.很容易看出值的符号很重要 - 无符号类型在某些计算中可以显着提高性能.以下是GCC 4.4.0
在我的盒子上的行为
Well, the compiler is not required to use any size. An easy thing to see is that the sign of the values matter - unsigned types can have significant performance boost in some calculations. The following is the behavior of GCC 4.4.0
on my box
int main(void) {
enum X { A = 0 };
enum X a; // X compatible with "unsigned int"
unsigned int *p = &a;
}
但是如果你分配一个-1
,那么GCC选择使用int
作为X
兼容的类型
But if you assign a -1
, then GCC choses to use int
as the type that X
is compatible with
int main(void) {
enum X { A = -1 };
enum X a; // X compatible with "int"
int *p = &a;
}
使用 GCC 的选项 --short-enums
,使其使用仍然适合所有值的最小类型.
Using the option --short-enums
of GCC, that makes it use the smallest type still fitting all the values.
int main() {
enum X { A = 0 };
enum X a; // X compatible with "unsigned char"
unsigned char *p = &a;
}
在最近的 GCC 版本中,编译器标志已更改为 -fshort-enums
.在某些目标上,默认类型是 unsigned int
.您可以查看答案 此处.
In recent versions of GCC, the compiler flag has changed to -fshort-enums
. On some targets, the default type is unsigned int
. You can check the answer here.
这篇关于总是 sizeof(enum) == sizeof(int) 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!