C++ 中枚举类型数据的大小是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8115550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:01:40  来源:igfitidea点击:

what is the size of an enum type data in C++?

c++enumssizeof

提问by user1002288

This is a C++ interview test question not homework.

这是一道 C++ 面试题,不是作业。

#include <iostream>
using namespace std;
enum months_t { january, february, march, april, may, june, july, august, september,    
  october, november, december} y2k;

 int main ()
  {
    cout << "sizeof months_t is  " << sizeof(months_t) << endl;
    cout << "sizeof y2k is  " << sizeof(y2k) << endl;
    enum months_t1 { january, february, march, april, may, june, july, august,    
       september, october, november, december} y2k1;
    cout << "sizeof months_t1 is  " << sizeof(months_t1) << endl;
    cout << "sizeof y2k1 is  " << sizeof(y2k1) << endl;
 }

Output:

输出:

sizeof months_t is 4
sizeof y2k is 4
sizeof months_t1 is 4
sizeof y2k1 is 4

sizeofmonth_t 是 4
sizeof
y2k 是 4 sizeofmonths_t1 是 4
sizeof y2k1 是 4

Why is the size of all of these 4 bytes? Not 12 x 4 = 48 bytes?
I know union elements occupy the same memory location, but this is an enum.

为什么所有这 4 个字节的大小都是如此?不是 12 x 4 = 48 字节?
我知道联合元素占用相同的内存位置,但这是一个枚举。

采纳答案by ObscureRobot

The size is four bytes because the enumis stored as an int. With only 12 values, you really only need 4 bits, but 32 bit machines process 32 bit quantities more efficiently than smaller quantities.

大小为四个字节,因为enum存储为int. 只有 12 个值,您实际上只需要 4 位,但 32 位机器比较小的数量更有效地处理 32 位数量。

0 0 0 0  January
0 0 0 1  February
0 0 1 0  March
0 0 1 1  April
0 1 0 0  May
0 1 0 1  June
0 1 1 0  July
0 1 1 1  August
1 0 0 0  September
1 0 0 1  October
1 0 1 0  November
1 0 1 1  December
1 1 0 0  ** unused **
1 1 0 1  ** unused **
1 1 1 0  ** unused **
1 1 1 1  ** unused **

Without enums, you might be tempted to use raw integers to represent the months. That would work and be efficient, but it would make your code hard to read. With enums, you get efficient storage and readability.

如果没有枚举,您可能会倾向于使用原始整数来表示月份。这将有效且高效,但它会使您的代码难以阅读。使用枚举,您可以获得高效的存储和可读性。

回答by Nicol Bolas

This is a C++ interview test question not homework.

这是一道 C++ 面试题,不是作业。

Then your interviewer needs to refresh his recollection with how the C++ standard works. And I quote:

然后你的面试官需要重新回忆 C++ 标准是如何工作的。我引用

For an enumeration whose underlying type is not fixed, the underlying type is an integral type that can represent all the enumerator values defined in the enumeration.

对于底层类型不固定的枚举,底层类型是一个整型,可以表示枚举中定义的所有枚举器值。

The whole "whose underlying type is not fixed" part is from C++11, but the rest is all standard C++98/03. In short, the sizeof(months_t)is not4. It is not 2 either. It couldbe any of those. The standard does not say what size it should be; only that it should be big enough to fit any enumerator.

整个“其底层类型不固定”部分来自C++11,其余都是标准的C++98/03。总之,sizeof(months_t)4.它不是2任一。它可以是其中任何一个。该标准没有说明它应该是什么尺寸;只是它应该足够大以适合任何枚举器。

why the all size is 4 bytes ? not 12 x 4 = 48 bytes ?

为什么所有的大小都是 4 个字节?不是 12 x 4 = 48 字节?

Because enums are not variables. The members of an enum are not actual variables; they're just a semi-type-safe form of #define. They're a way of storing a number in a reader-friendly format. The compiler will transform all uses of an enumerator into the actual numerical value.

因为枚举不是变量。枚举的成员不是实际变量;它们只是#define 的半类型安全形式。它们是一种以读者友好的格式存储数字的方法。编译器会将枚举器的所有使用转换为实际数值。

Enumerators are just another way of talking about a number. januaryis just shorthand for 0. And how much space does 0 take up? It depends on what you store it in.

枚举数只是谈论数字的另一种方式。january只是 的简写0。0 占用多少空间?这取决于你把它存放在什么地方。

回答by celtschk

It depends. The standard only demands that it is large enough to hold all values, so formally an enum like enum foo { zero, one, two };needs to only be one byte large. However most implementations make those enums as large as ints (that's faster on modern hardware; moreover it's needed for compatibility with C where enums are basically glorified ints). Note however that C++ allows enums with initializers outside of the int range, and for those enums the size will of course also be larger. For example, if you have enum bar { a, b = 1LL << 35 };then your enum will be larger than 32 bits (most likely 64 bits) even on a system with 32 bit ints (note that in C that enum would not be allowed).

这取决于。该标准只要求它足够大以容纳所有值,因此形式上的 enum likeenum foo { zero, one, two };只需要一个字节大。然而,大多数实现使这些枚举与整数一样大(这在现代硬件上更快;此外,为了与 C 兼容,枚举基本上是美化的整数)。但是请注意,C++ 允许使用 int 范围之外的初始化器的枚举,对于这些枚举,大小当然也会更大。例如,如果您有,enum bar { a, b = 1LL << 35 };那么即使在具有 32 位整数的系统上,您的枚举也将大于 32 位(很可能是 64 位)(请注意,在 C 中不允许使用该枚举)。

回答by agenttom

An enum is kind of like a typedef for the int type (kind of).

枚举有点像 int 类型(种类)的 typedef。

So the type you've defined there has 12 possible values, however a single variable only ever has one of those values.

所以你在那里定义的类型有 12 个可能的值,但是一个变量只有这些值之一。

Think of it this way, when you define an enum you're basically defining another way to assign an int value.

这样想,当您定义枚举时,您基本上是在定义另一种分配 int 值的方法。

In the example you've provided, january is another way of saying 0, feb is another way of saying 1, etc until december is another way of saying 11.

在您提供的示例中,january 是表示 0 的另一种方式,feb 是表示 1 的另一种方式,依此类推,直到 12 月是表示 11 的另一种方式。

回答by thedaver64

With my now ageing Borland C++ Builder compiler enums can be 1,2 or 4 bytes, although it does have a flag you can flip to force it to use ints.

随着我现在老化的 Borland C++ Builder 编译器枚举可以是 1,2 或 4 个字节,尽管它确实有一个标志,您可以翻转以强制它使用整数。

I guess it's compiler specific.

我想这是特定于编译器的。

回答by Stuart Golodetz

Because it's the size of an instance of the type - presumably enum values are stored as (32-bit / 4-byte) ints here.

因为它是类型实例的大小 - 大概枚举值在这里存储为(32 位/4 字节)整数。

回答by Anton Rasmussen

I like the explanation From EdX (Microsoft: DEV210x Introduction to C++) for a similar problem:

我喜欢来自 EdX (Microsoft: DEV210x Introduction to C++) 对类似问题的解释:

"The enum represents the literal values of days as integers. Referring to the numeric types table, you see that an int takes 4 bytes of memory. 7 days x 4 bytes each would require 28 bytes of memory if the entire enum were stored but the compiler only uses a single element of the enum, therefore the size in memory is actually 4 bytes."

“枚举将天的字面值表示为整数。参考数字类型表,您会看到一个 int 占用 4 个字节的内存。如果存储了整个枚举,则 7 天 x 4 个字节每个将需要 28 个字节的内存,但是编译器只使用枚举的一个元素,因此内存中的大小实际上是 4 个字节。”

回答by Basile Starynkevitch

An enum is nearly an integer. To simplify a lot

枚举几乎是一个整数。简化很多

enum yourenum { a, b, c };

is almost like

几乎就像

#define a 0
#define b 1
#define c 2

Of course, it is not really true. I'm trying to explain that enum are some kind of coding...

当然,事实并非如此。我试图解释枚举是某种编码......