Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enum Size *in practice*

Tags:

c++

For C++ prior 2011, the standard says that enums can be any size, from byte to long long. But in practice it seems most compilers make them ints, 4 bytes.

So, in practice do any vaguely current compilers not make them ints?

And I seem to need to clarify that I am not doing anything weird, like enums > 2^31. Just simple enums. And on 32 or 64 bit systems, my software will not run on 16 bits!

like image 384
Tuntable Avatar asked May 18 '16 07:05

Tuntable


People also ask

What will be the size of enum?

On an 8-bit processor, enums can be 16-bits wide. On a 32-bit processor they can be 32-bits wide or more or less. The GCC C compiler will allocate enough memory for an enum to hold any of the values that you have declared. So, if your code only uses values below 256, your enum should be 8 bits wide.

Why is enum size 4?

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.

What is enum in C++ with example?

Enumeration is a user defined datatype in C/C++ language. It is used to assign names to the integral constants which makes a program easy to read and maintain. The keyword “enum” is used to declare an enumeration. The following is the syntax of enums. enum enum_name{const1, const2, ....... };

What is enum type in C++?

An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators. This article covers the ISO Standard C++ Language enum type and the scoped (or strongly-typed) enum class type which is introduced in C++11.


2 Answers

Let's see it on any modern compiler:

#include <iostream>
#include <limits>

enum MySmallSmall {
  SmallValue = 0,
};

enum MyLongLong {
  LongValue = std::numeric_limits<long long>::max()
};

int main() {
  std::cout << "sizeof MySmallSmall is " << sizeof(MySmallSmall) << std::endl;
  std::cout << "sizeof MyLongLong is " << sizeof(MyLongLong) << std::endl;
  return 0;
}

clang and g++ output:

sizeof MySmallSmall is 4

sizeof MyLongLong is 8

But for MS Visual Studio both results are 4 (I've checked it using this site http://rextester.com/l/cpp_online_compiler_visual not sure what compiler version is here)

So you can't rely on sizeof of any enum.

like image 183
Vadim Key Avatar answered Oct 18 '22 02:10

Vadim Key


If an enum was always an int, then the following would be unsafe:

enum oops {
    foo = 32767,
    bar, /*what am I?*/
};

This is because an int can be as small as 16 bits (and that is still surprisingly common). On a system with a 32 bit int, you could set foo = 2147483647 and your compiler most certainly wouldn't pick an int as the underlying type.

So the clever C++ bods specify that the underlying enum integral type must be capable of representing the values given, and a compiler is free to pick one that's appropriate. Given that int is often considered as the machine's native type, it's often a sensible choice.

If you want to know the underlying type of an enum, then std::underlying_type provides this.

like image 37
Bathsheba Avatar answered Oct 18 '22 02:10

Bathsheba