-2

What would be the size of this enum, in bytes? C++

enum Cars { Toyota, Suzuki, Volkswa, Mitsubish, Alfarome, Holden, Bradleys };
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
Wulfinite
  • 209
  • 1
  • 3
  • 9

3 Answers3

11

From §7.2/5 of the C++ standard, you have that in your case the underlying type is not fixed:

The underlying type can be explicitly specified using an enum-base. For a scoped enumeration type, the underlying type is int if it is not explicitly specified. In both of these cases, the underlying type is said to be fixed. [...]

Which leads to §7.2/7:

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. If no integral type can represent all the enumerator values, the enumeration is ill-formed. It is implementation-defined which integral type is used as the underlying type except that the underlying type shall not be larger than int unless the value of an enumerator cannot fit in an int or unsigned int. If the enumerator-list is empty, the underlying type is as if the enumeration had a single enumerator with value 0.


Summing up, for your enumeration the underlying type is at most an int or unsigned int. You can check the size via sizeof, and the type via typeid. Example checking code that prettifies the result with g++ (not necessary with Visual C++):

#include <iostream>
#include <typeinfo>     // std::type_info
#include <type_traits>  // std::underlying_type
using namespace std;

int const bits_per_byte = CHAR_BIT;

#ifdef __GNUC__
#   include <cxxabi.h>      // abi::*, free
#   include <string>        // std::string

    auto display_name( type_info const& info )
        -> string
    {
        int   status;
        char* demangled = abi::__cxa_demangle( info.name(), 0, 0, &status );
        string result = demangled;
        free( demangled );
        return result;
    }
#else
    auto display_name( type_info const& info )
        -> string
    { return info.name(); }    
#endif // __GNUC__

enum Cars {
    Toyota, Suzuki, Volkswa, Mitsubish, Alfarome, Holden, Bradleys
    };

auto main() -> int
{
    using Cars_type = typename underlying_type< Cars >::type;
    type_info const& info = typeid( Cars_type );
    cout << "This compiler is " << bits_per_byte*sizeof(void*) << "-bit.\n";
    cout << "Underlying type is '" << display_name( info ) << "'.\n";
    cout << "Size = " << sizeof( Cars ) << " bytes.\n";
}

Output with MinGW g++ (tdm64-1) 5.1.0:

This compiler is 64-bit.
Underlying type is 'unsigned int'.
Size = 4 bytes.
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • Well it's not a question that I formed, just a random question a friend gave me, and I answered 8, which was wrong. The remaining options are 7, 28 and 32. Which would you think it would be? – Wulfinite Oct 05 '15 at 14:17
  • @Wulfinite: None of the above. – Fred Larson Oct 05 '15 at 14:18
  • 6
    @Wulfinite Tell your friend the question is not well posed – Brandlingo Oct 05 '15 at 14:18
  • 4
    @Wulfinite: Utter nonsense. – Fred Larson Oct 05 '15 at 14:20
  • Well ask your friend! – Brandlingo Oct 05 '15 at 14:20
  • 4
    It's not.​​​​​​ Your friend is making up complete rubbish. – Lightness Races in Orbit Oct 05 '15 at 14:20
  • This 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. This is the only possible way of looking at it. – Wulfinite Oct 05 '15 at 14:21
  • 1
    @Wulfinite: That's also complete rubbish. Where are you getting this crap? – Lightness Races in Orbit Oct 05 '15 at 14:22
  • 2
    @Wulfinite: Lightness is right, what you state is complete rubbish, *when we look at it with good knowledge of how things work*. However it might appear to make sense in some flawed conceptual model. The question is then exactly how flawed: if it is infinitely recursive ignorance then it's, like, bad, but otherwise, you can gain some insight by e.g. studying generated assembly code. – Cheers and hth. - Alf Oct 05 '15 at 14:28
  • @Wulfinite: Could it be that the entire misunderstanding here is the misconception that type sizes are fixed by the C++ standard? Well, they are not. There are minimum sizes and some guarantees about certain types being at least as big as others, but that's all. This is completely unlike e.g. Java. – Christian Hackl Oct 05 '15 at 15:11
  • C, not C++, but very similar to your rubbish question. The guy also has a wrong premise about enum. [How can i know the size of the enum Days? Will it be equal to 7*4(sizeof(int)) = 28](http://stackoverflow.com/q/9972345/995714). How does it take 7*4 bytes when it can only store 1 value at a time, not all 7 values. It's not a [set](http://en.cppreference.com/w/cpp/container/set) type – phuclv Oct 05 '15 at 15:17
7

The size depends on the platform/implementation.

In C++11, you can specify the underlying type (and hence the size). In the following example the size would be 1 byte:

#include <iostream>
#include <cstdint>

enum cars : std::uint8_t {
    Toyota, Suzuki, Volkswa, Mitsubish, Alfarome, Holden, Bradleys
};

int main() {
    cars my_car = cars::Toyota;
    std::cout << sizeof(my_car) << std::endl;
};

Returns:

1

While

enum cars : std::uint32_t { ...

Returns:

4
sergej
  • 17,147
  • 6
  • 52
  • 89
  • 1
    I think the OP should run this code, with and without specifying the underlying type. That should prove that none of the multiple choice answers are correct. – Fred Larson Oct 05 '15 at 14:33
1

Each enumeration is a separate entity.

The compiler needs to allocate enough storage to hold the highest value of the enumeration.

In your case, there are 7 entities. The compiler needs to allocate enough storage to hold the value 7.

The minimum size of an addressable data type is 1.

If you create a variable of the enum type, it must have a minimum size of 1. However, the compiler can choose a size that is more optimal for the architecture (platform), such as 2 bytes (16-bit word), 4 bytes (32-bit word) or other.

If the enumeration items are used individually, like in an if statement, the compiler may not allocate any data storage and emit the values into an instruction in the executable.

So the answer to your question is: the minimum space for a variable of the enum type is 1. What your compiler uses is dependent on the compiler and the platform. If the enumeration value is used in an expression, the compiler may choose to put the value in the instruction, so there will be zero data space allocated.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154