where and how are the values in each of the members of enum stored in C?

浪尽此生 提交于 2021-01-28 03:17:14

问题


The way structures allocate memory is that :-

struct mys
{ 
   int a , b, c ;
};

As we can see in structs, when I declare a struct variable say, struct mys var1, var1 takes the sum of all the basic datatypes inside it. (12 bytes here assuming the word length is 4 bytes) printf("%d",sizeof(var1)) ; output is 4.


In enum, we have, enum myvar{ id1 , id2 , id3 }; and whenever I declare an enum variable and print its size, it only prints the size of integer(4 bytes). And the id1 ,id2, id3 would get 0 to 2 consecutively. So i thought it's analogous to generators in python (actual allocation of memory is just 2 bytes) and it adds 1 to each of the consecutive members of the enum type when we access it.

But, what confused me about enum is that, even though we define an enum like this :-

enum myvar{ id1 = 20 , id2 =42, id3=1 };

If I declare an enum variable enum myvar var1 , then var1 would still take 4 bytes of memory. Where are the values that I have given in the definition getting stored? Since I had given random values for the members of the enum, I thought it would allocate 6 bytes of memory since it's not the usual 0 to 2 default integers anymore after assigning them. So clearly I'm wrong. What is the reason behind this? If the size of enum is just the word length , how does it manage the memory allocation . Clear explanations please ......


回答1:


An enum type in C is basically a logical grouping of named int constants. An enum variable is only guaranteed to hold a single int value, regardless of how many named constants the type contains. And there isn't a way to "enumerate" all possible enum values in C. If you are using a compiler and architecture where int is 32-bit, all values of this enum (named constants) will have to fit inside these 4 bytes.

Additionally, the variable doesn't even have to hold any of the constants defined in the enum type at runtime, it will behave as a plain int variable. Some compilers won't even throw a warning if you mix different enum types, at least not until you enable additional warnings.

So, actual values won't be stored anywhere after compilation (unless they are used), just as a #define macro won't be stored anywhere unless you reference it somehow.

So, when you write this:

// define the enum 
enum my_enum { 
    id1 = 20,
    id2 = 42,
    id3 = 1 
};

// declare the variable 'my_val' and assign 'id2'
enum my_enum my_val = id2;

It will be almost equivalent to:

#define id1 20
#define id2 42
#define id3 1

int my_val = id2;

And the compiler will behave as you simply wrote:

int my_val = 42;

and throw everything else away.

So, if you are asking where 42 is stored, then the answer is, somewhere between the instructions inside the code section of your executable. If you didn't use id1 and id3 anywhere, they won't exist anywhere.

If you need to store a list of values in C, you'll either need to use an array of integers, an array of structs, or a more elaborate data structure (linked list, hash table, a tree, or whatever suits your use case).




回答2:


You use enum to define a set of values allowed to be used in a variable of that type; as only one of them at a time can be in there, the size is what would be needed to hold that, regardless of how many possibilities there are.




回答3:


The identifiers associated to values of en enumeration only exist in translation time. They are replaced by their numerical values during compilation, so they are not held anywhere as objects in storage space.

Ulterior variables declared as having the enumeration type you defined there will have an appropriate integer type, chosen by the compiler. Thus, you can assign integer values to these objects, not previously named in the enumeration.



来源:https://stackoverflow.com/questions/45845557/where-and-how-are-the-values-in-each-of-the-members-of-enum-stored-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!