sizeof(enum) == sizeof(int) 总是吗? [英] Is the sizeof(enum) == sizeof(int), always?

查看:21
本文介绍了sizeof(enum) == sizeof(int) 总是吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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."

"So it is implementation-defined, and sizeof(enum) might be equal to sizeof(char), i.e. 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 中还可能包含扩展的整数类型(但是,如果提供了它们,实现必须记录下来).枚举的类型是某种可以存储任何枚举数的值的类型(本例中为 AB).

    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) 总是吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆