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

前端 未结 8 461
旧巷少年郎
旧巷少年郎 2020-12-01 02:51

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

#include 
using namespace std;
enum months_t { january, february, march, april, may, jun         


        
相关标签:
8条回答
  • 2020-12-01 02:58

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

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

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

    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.

    0 讨论(0)
  • 2020-12-01 03:02

    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.

    I guess it's compiler specific.

    0 讨论(0)
  • 2020-12-01 03:04

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

    0 讨论(0)
  • 2020-12-01 03:07

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

    0 讨论(0)
  • 2020-12-01 03:17

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

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

    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.

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

    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.

    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)
  • 2020-12-01 03:22

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

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

    0 讨论(0)
提交回复
热议问题