1

Suppose I have the following enum declared somewhere:

enum yin_yang { yin, yang }; 

And I wish to use the new data type like so:

yin_yang balance = yang; 

How much of the system's memory will 'balance' be occupying? 1-bit, 1 byte or something else?

5
  • 1
    Possible duplicate of size of enum is constant no matter how many element it contains Commented Oct 7, 2015 at 9:09
  • 1
    check it out with cout << sizeof( yin_yang ) << endl; Commented Oct 7, 2015 at 9:13
  • You can rule out the possibility that it consumes only one bit. sizeof(yin_yang) has to be at least one, that is size of a char and a char has to be more than a bit.. Commented Oct 7, 2015 at 9:13
  • 1
    A complete object cannot consume "1 bit". By definition, the smallest addressable unit that you can express in C++ is a "byte", or more accurately a char (which may contain an arbitrary number of bits, but at least eight). Data smaller than a char either contains padding (e.g. a bool) or shares a storage location with other data (e.g. a bitfield). Commented Oct 7, 2015 at 9:13
  • Thanks for the replies everyone. Sorry for the duplicate question since I wasn't sure what to Google. After messing around with sizeof() it's depressing to know that even a bool is consuming 1 byte. Commented Oct 7, 2015 at 10:23

5 Answers 5

4

Whether it actually occupies memory depends on what the optimizer does with your code, but if you want to know the size in bytes of a type or object, you can use the sizeof operator:

std::cout << sizeof(yin_yang) << std::endl; std::cout << sizeof(balance) << std::endl; 

This doesn't mean that balance actually has to occupy space in memory at runtime. It can be completely optimized away. But the size the object would have is known at compile time.

Sign up to request clarification or add additional context in comments.

1 Comment

yes, although the compiler can completly optimize away the value but still print it's alleged size
3

At least in C++11, you can specify the integer type that you want to use as the basis for the enum:

enum yin_yang : uint8_t { yin, yang }; 

Otherwise the compiler chooses the base type, which will be an integer type, though it is not specified which IIRC (and I believe many compilers default to int). The base type determines the storage requirements of an enum instance.

1 Comment

The compilers are free to choose any type not larger than int.
0

You could use the unary operator 'sizeof' to calculate the size of any variable or datatype, measured in the number of byte size units.

Comments

0

For scoped enums, the relevant part excerpted from C++14, [decl.enum]:

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.

So if enumerated states are representable in an int then it is guaranteed to be ≤ sizeof(int), greater if not. Of course, if you've explicitly specified the underlying type, then you know know its size.

Comments

0

An enum type is a special data type that enables for a variable to be a set of predefined constants.It doesn't matter how many unit it has

For instance,

enum Days { Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday };

enum Letter { a,b,c,d};

Above both enum data size are just 4 bytes

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.