1
typedef enum enumStruc_2 {
    ENUM_5 = 0xFFFFFFF0, ENUM_6, ENUM_7, ENUM_8
} enumStruc_2;

Here, size of enum is 4 bytes(Integer size). How it saves all of its enum member values within 4 bytes? Is always enum size 4 bytes irrespective of value of its member?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • There are exactly four distinct sequential values of the `enum`. To be able to represent all of four distinct values requires no more than two bits. Since the compiler needs to see the enum to use it, it could easily represent it (in effect) using an `int` as the underlying type. If the native `int` on your platform is 32-bit, that would explain `sizeof` giving a result of 4. And, no, the size of enum types are not guaranteed to be 4. Their sizes are implementation defined - the standard is silent on the subject of what their size should be. – Peter Sep 20 '19 at 12:29
  • 1
    The question is related to C – Surendar Sekar Sep 20 '19 at 12:31
  • Please don't edit out language tags when there are already answers in place. Yes, this should have been tagged C only, but it its too late to fix once there are C++ answers posted. For cases with both C and C++ tags, please consider the tag moderation guidelines in the [tag wiki](https://stackoverflow.com/tags/c%2b%2b/info). – Lundin Sep 20 '19 at 12:55

4 Answers4

4

It's up to the implementation how big an enum is. From section 6.7.2.2p4 of the C standard regarding Enumeration specifiers:

Each enumerated type shall be compatible with char , a signed integer type, or an unsigned integer type.
The choice of type is implementation-defined, but shall be capable of representing the values of all the members of the enumeration. The enumerated type is incomplete until immediately after the } that terminates the list of enumerator declarations, and complete thereafter.

So the size of an enum type could be the size of an int, char, or any other integer type. As an example, gcc sizes enums as follows:

Normally, the type is unsigned int if there are no negative values in the enumeration, otherwise int. If -fshort-enums is specified, then if there are negative values it is the first of signed char, short and int that can represent all the values, otherwise it is the first of unsigned char, unsigned short and unsigned int that can represent all the values.

As for the values of each member, paragraph 3 states:

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such
are permitted. An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no = , the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.

In this case, the constant ENUM_5 is explicitly assigned the value 0xFFFFFFF0, so ENUM_6 is 0xFFFFFFF, ENUM_7 is 0xFFFFFFF2, and ENUM_8 is 0xFFFFFFF3. Each of these constants needs 4 bytes to store the value, so this particular enum must be at least 4 bytes in size, although it could be larger.

Regarding:

How it saves all of its enum member values within 4 bytes?

It doesn't hold all 4 members at once. An enum can hold one of those 4 members. You're defining a type, not a value. For example:

enum enumStruc_2 e1 = ENUM_5;
enum enumStruc_2 e2 = ENUM_6;
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Why its memory size is not 4*sizeof(int) when it is holding 4 members in it? – Surendar Sekar Sep 20 '19 at 12:42
  • 1
    @SurendarSekar It doesn't hold all 4 members at once. An enum can hold *one of* those 4 members. You're defining a type, not a value. For example `enum enumStruc_2 e1 = ENUM_5`. – dbush Sep 20 '19 at 12:44
  • @SurendarSekar Glad I could help. Feel free to [accept this answer](https://stackoverflow.com/help/accepted-answer) if you found it useful. – dbush Sep 20 '19 at 13:14
2

In both C and C++, the size of an enumerator sizeof(enumStruc_2) is the size of any individual element in that enumeration.

In C, the answer is sizeof(int). So that's at least 2.

In C++, the answer is sizeof(std::underlying_type<enumStruc_2>::type).

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
2

The size of a type is not the size needed to store all the possible values of that type. It is the size needed to store one instance of that type.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • How it stores 0xFFFFFFF0,0xFFFFFFF1,0xFFFFFFF2,0xFFFFFFF3 these 4 values within 4 bytes? – Surendar Sekar Sep 20 '19 at 12:33
  • 4
    Asking "how the enum stores 4 values" is like asking "how `int` stores all its millions of possible values". It doesn't. _One_ integer stores _one_ integer value. If you want to store 40 integer values, you'll need 40 integers. In the same way, _one_ variable of type enum stores _one_ of the possible values of the enum, and it needs to have enough size to do that. Your parking space needs to fit one car, not all the cars you could possibly buy. – Daniel Daranas Sep 20 '19 at 12:36
  • 2
    It's also worth noting that "one of the possible values of the enum" is not _only_ the values you listed in the enum's definition. Those are just shortcuts. It can still legally hold any value of its underlying type (e.g. `int`). – Lightness Races in Orbit Sep 20 '19 at 13:12
  • Yes you are right. Is those enum members name replaced with original constant integer value during preprocessing time like #define? – Surendar Sekar Sep 20 '19 at 13:42
0

When you declare an enum you do two things. First is you declare a new type that has the name of the enum. This type is an integer type that is large enough to store the largest value of the values you use inside the enum and it will not be smaller than an int unless you specify so.

The next thing you do is declare 0 or more values of that type that are constants. These don't participate in the size of the enum like class member variables as they aren't really variables. They are just named constants that you can use and they have the type of the enum type. This is why you can have as many enum values and the sizeof(enum_name) will never change.

NathanOliver
  • 171,901
  • 28
  • 288
  • 402