What is the size of enum type data in C ++?

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

#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 - 4
sizeof months_t1 - 4
sizeof y2k1 is 4

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

+57
c ++ enums sizeof
Nov 13 '11 at 23:13
source share
8 answers

The size is four bytes, since 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 ones.

 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 listing, you might be tempted to use raw integers to represent months. This will work and be effective, but it will make your code difficult to read. With transfers, you get efficient storage and readability.

+46
Nov 13 '11 at 23:16
source share

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

Then your interviewer should brush up on how the C ++ standard works. And I quote :

For an enumeration whose base type is not fixed, the base type is an integer type that can represent all the values ​​of the enumerator defined in the enumeration.

The entire part, whose "base type is not fixed", is taken from C ++ 11, but all the rest are standard C ++ 98/03. In short, sizeof(months_t) not 4. It is also not 2. It can be any of them. The standard does not say how large it should be; only that it should be large enough to fit any enumerator.

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

Because enumerations are not variables. The members of an enumeration are not actual variables; it's just a semi-safe form of #define. This is a way to store numbers in a reader-friendly format. The compiler converts all uses of the enumerator to an actual numeric value.

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

+99
Nov 14 '11 at 0:11
source share

It depends. The standard only requires that it be large enough to hold all values, so a formal enumeration of the type enum foo { zero, one, two }; must be only one byte. However, most implementations make these enums as large as ints (which is faster on modern hardware, moreover, for compatibility with C, where enums are mostly glorified by ints). Note, however, that C ++ allows enumerations with initializers outside the int range, and for these enumerations, the size will of course also be larger. For example, if you have enum bar { a, b = 1LL << 35 }; , then your enumeration will be more than 32 bits (most likely 64 bits) even on a system with 32-bit ints (note that in C this enumeration will not be allowed).

+9
Nov 13 '11 at 23:26
source share

An enumeration is kind of typedef for type int (type).

So, the type that you defined there has 12 possible values, however, one variable has only one of these values.

Think about it this way when you define an enumeration, you basically define another way to assign an int value.

In the example you pointed out, January is another way to say 0, feb is another way to say 1, etc. until December becomes another option 11.

+6
Nov 13 '11 at 23:18
source share

Since the size of the type instance is supposedly the enumeration values ​​are stored here (32-bit / 4-byte) ints here.

+4
Nov 13 '11 at 23:16
source share

With my deprecated Borland C ++ Builder compiler lists, there may be 1.2 or 4 bytes, although it does have a flag that you can flip over to force it to use ints.

I assume this is a compiler.

+4
Apr 25 2018-12-12T00:
source share

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

"The enumeration is the literal values ​​of the days as integers. Referring to the table of numeric types, you see that int takes 4 bytes of memory. For 7 days x 4 bytes, each of them will require 28 bytes of memory, if the whole enumeration was but the compiler only uses one element of an enumeration, so the size in memory is actually 4 bytes. "

+3
Mar 22 '17 at 19:40
source share

The listing is almost integer. To simplify a lot

 enum yourenum { a, b, c }; 

almost like

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

Of course, this is not so. I am trying to explain that enumeration is a kind of coding ...

+1
Nov 13 '11 at 23:18
source share



All Articles