What would be the size of this enum, in bytes? C++
enum Cars { Toyota, Suzuki, Volkswa, Mitsubish, Alfarome, Holden, Bradleys };
What would be the size of this enum, in bytes? C++
enum Cars { Toyota, Suzuki, Volkswa, Mitsubish, Alfarome, Holden, Bradleys };
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.
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
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.