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

时间:2021-06-05 16:38:29

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

sizeof months_t是4 sizeof y2k是4 sizeof months_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字节?我知道union元素占用相同的内存位置,但这是一个enum。

8 个解决方案

#1


40  

The size is four bytes because the enum is 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.

它的大小是4个字节,因为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.

如果没有枚举,您可能会试图使用原始整数来表示月份。这样做既有效又有效,但会使代码难于阅读。使用enum,您可以获得高效的存储和可读性。

#2


85  

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 not 4. It is not 2 either. It could be 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.

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

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

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

#3


9  

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 foo {0, 1, 2};只需要一个字节大。然而,大多数实现使得这些enums与ints一样大(在现代硬件上速度更快;此外,它还需要与C兼容,而在C中,enums基本上是被美化的ints)。但是,请注意,c++允许在int范围之外使用初始化器,对于那些enums,大小当然也会更大。例如,如果您有enum bar {a, b = 1LL << 35};然后你的enum将大于32位(很可能是64位),即使是在一个有32位输入的系统上(请注意,在C中enum是不允许的)。

#4


5  

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

enum类似于int类型的类型定义(有点类似)。

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.

这样想,当定义enum时,基本上是定义另一种分配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.

在你提供的例子中,1是表示0的另一种方式,2是表示1的另一种方式,直到12月是表示11的另一种方式。

#5


4  

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字节)ints。

#6


3  

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编译器enums可以是1、2或4字节,尽管它确实有一个标记,您可以通过翻转来强制它使用ints。

I guess it's compiler specific.

我猜它是特定于编译器的。

#7


3  

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

我喜欢EdX (Microsoft: DEV210x 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."

enum将天数的文字值表示为整数。参照数字类型表,您可以看到int占用4字节的内存。如果整个enum都被存储,那么每个7天x4字节将需要28字节的内存,但是编译器只使用enum的单个元素,因此内存中的大小实际上是4字节。

#8


2  

An enum is nearly an integer. To simplify a lot

enum几乎是一个整数。为了简化很多

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

当然,这并不是真的。我试图解释enum是某种编码……

#1


40  

The size is four bytes because the enum is 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.

它的大小是4个字节,因为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.

如果没有枚举,您可能会试图使用原始整数来表示月份。这样做既有效又有效,但会使代码难于阅读。使用enum,您可以获得高效的存储和可读性。

#2


85  

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 not 4. It is not 2 either. It could be 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.

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

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

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

#3


9  

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 foo {0, 1, 2};只需要一个字节大。然而,大多数实现使得这些enums与ints一样大(在现代硬件上速度更快;此外,它还需要与C兼容,而在C中,enums基本上是被美化的ints)。但是,请注意,c++允许在int范围之外使用初始化器,对于那些enums,大小当然也会更大。例如,如果您有enum bar {a, b = 1LL << 35};然后你的enum将大于32位(很可能是64位),即使是在一个有32位输入的系统上(请注意,在C中enum是不允许的)。

#4


5  

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

enum类似于int类型的类型定义(有点类似)。

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.

这样想,当定义enum时,基本上是定义另一种分配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.

在你提供的例子中,1是表示0的另一种方式,2是表示1的另一种方式,直到12月是表示11的另一种方式。

#5


4  

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字节)ints。

#6


3  

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编译器enums可以是1、2或4字节,尽管它确实有一个标记,您可以通过翻转来强制它使用ints。

I guess it's compiler specific.

我猜它是特定于编译器的。

#7


3  

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

我喜欢EdX (Microsoft: DEV210x 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."

enum将天数的文字值表示为整数。参照数字类型表,您可以看到int占用4字节的内存。如果整个enum都被存储,那么每个7天x4字节将需要28字节的内存,但是编译器只使用enum的单个元素,因此内存中的大小实际上是4字节。

#8


2  

An enum is nearly an integer. To simplify a lot

enum几乎是一个整数。为了简化很多

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

当然,这并不是真的。我试图解释enum是某种编码……