C中的每个枚举成员的值存储在哪里以及如何存储? [英] where and how are the values in each of the members of enum stored in C?

查看:63
本文介绍了C中的每个枚举成员的值存储在哪里以及如何存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结构分配内存的方式是:-

The way structures allocate memory is that :-

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

正如我们在struct中看到的那样,当我声明一个struct变量说 struct mys var1 时,var1接受其中的所有基本数据类型的总和.(假设字长为4个字节,此处为12个字节) printf(%d",sizeof(var1)); 输出为 4 .

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.

在枚举中,我们有 enum myvar {id1,id2,id3}; ,每当我声明一个enum变量并显示其大小时,它仅显示整数(4个字节)的大小.并且id1,id2,id3将连续获得0到2.因此,我认为它类似于python中的生成器(实际的内存分配仅为2个字节),并且在我们访问它时,它会为enum类型的每个连续成员加1.

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.

但是,令我们困惑的是 enum ,即使我们定义了这样的enum:-

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

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

如果我声明一个枚举变量 enum myvar var1 ,则var1仍将占用4个字节的内存.我在定义中给出的值存储在哪里?由于我为枚举的成员提供了随机值,因此我认为它将分配6个字节的内存,因为分配它们后,它不再是通常的0到2个默认整数.很明显我错了.这背后的原因是什么?如果enum的大小仅是字长,那么它将如何管理内存分配.明确的解释请......

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 ......

推荐答案

C中的 enum 类型基本上是名为 int 常量的逻辑分组.不论类型包含多少个命名常量, enum 变量仅保证保留单个 int 值.而且没有办法枚举" C中所有可能的枚举值.如果您使用的是 int 是32位的编译器和体系结构,则该枚举的所有值(称为常量)将必须容纳在这4个字节中.

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.

此外,该变量甚至不必在运行时保留枚举类型中定义的任何常量,它将表现为普通的 int 变量.如果您混合使用不同的 enum 类型,某些编译器甚至不会发出警告,至少直到启用其他警告为止.

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.

因此,编译后实际值不会存储在任何地方(除非使用它们),就像 #define 宏不会存储在任何地方,除非您以某种方式引用它.

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;

这几乎等同于:

#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;

把其他东西扔掉.

因此,如果您要询问 42 的存储位置,那么答案就在可执行文件的代码部分内部的指令之间.如果您不在任何地方使用 id1 id3 ,那么它们将在任何地方都不存在.

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.

如果需要在C中存储值列表,则将需要使用整数数组,结构数组或更复杂的数据结构(链接列表,哈希表,树或其他内容)适合您的用例).

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).

这篇关于C中的每个枚举成员的值存储在哪里以及如何存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆