Titone Maurice Titone Maurice - 1 year ago 76
C Question

Do enums have storage space when they're not instantiated?

If I don't instantiate an enum, and just have:

enum myEnum {val1 = 0, val2, val2};

I'm wondering if all the compiler will do is go through all your code and replace val1, val2 etc. with the corresponding numbers. Meaning it wouldn't create storage for it in your finished program. Actually the final result would be constant values substituted wherever you wrote val1, val2 etc.?

I'm not sure if "instantiate" is the right word. Also, when I say it goes through your code and replaces stuff, I'm not saying it pastes it in like with macro replacement at the preprocessor stage. Thanks.

Answer Source

The enumerators of an enumeration are prvalues, so when you use val1 in the source code, it is similar to typing out a literal such as 42; you do not get a glvalue (which would refer to a location in memory where the actual value is stored). It wouldn't make any sense for the compiler to reserve space in the program for the enumerators since those memory locations would never be accessed according to the rules of the language.

Then again, are you really so short on memory that this matters?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download