需要帮助理解枚举和数组

时间:2021-07-23 02:01:43

I use the following two lines of code:

我使用以下两行代码:

enum bus_sigs {  REG0, REGA, REGB, REGC, REGD };
short bus[5];

The purpose is to index the array of shorts with the enum names. For example, I use: bus[REGA].

目的是使用枚举名称索引短裤数组。例如,我使用:bus [REGA]。

I am getting weird behavior: if I use bus[REGC] I am getting weird values as if I am fetching data from beyond the array memory range. If I then update the second line to:

我得到了奇怪的行为:如果我使用总线[REGC]我会得到奇怪的值,好像我从数组内存范围之外获取数据。如果我然后将第二行更新为:

short bus[10];

The behavior is again as expected. But this makes no sense to me. I am only ever assigning to 5 members in the bus array.

行为再次如预期。但这对我来说毫无意义。我只分配给总线阵列中的5个成员。

What am I not getting?

我得不到什么?

3 个解决方案

#1


0  

Have you initialized the array yet? Just writing short bus[5]; will allocate the memory for 5 short variables but that's all.

你有没有初始化阵列?只写短巴士[5];将为5个短变量分配内存,但这就是全部。

What you can do is short bus[5] = {0}; and it will initialize all entries to 0 so you know exactly what you're dealing with ahead of time.

您可以做的是短总线[5] = {0};它会将所有条目初始化为0,这样您就可以确切地知道您正在处理的是什么。

#2


2  

It's hard to tell because you haven't included any relevant code. But here are some ways you could get a wrong value:

很难说,因为您没有包含任何相关代码。但是这里有一些方法可以得到错误的值:

  1. You use a pointer of a wrong size and read past the end of the array:

    您使用大小错误的指针并读取数组的末尾:

    long* b = (long*)bus;
    result = b[4]; // this will read past the end of the array
    
  2. You copy from other location into the bus array, but use a wrong count:

    您从其他位置复制到总线阵列,但使用错误的计数:

    short bus[5];
    
    // this is 5
    int count = sizeof(bus)/sizeof(bus[0]);
    
    // this will copy 5 bytes, instead of 5 shorts
    // (this is why bus[10] would fix the issue, for example)
    memcpy(bus, some_location, count);
    
  3. You never initialize the array, but read the value before writing to it:

    您永远不会初始化数组,但在写入之前读取该值:

    // this *doesn't* clear the contents of the array
    short bus[5];
    
    // value of result is undefined
    result = bus[4];
    

#3


0  

To clarify: When you declare enum bus_sigs { REG0, REGA, REGB, REGC, REGD }; you are basically saying that each of these keywords gets the following assignment within the enum's scope:

澄清一下:当你声明enum bus_sigs {REG0,REGA,REGB,REGC,REGD}时;您基本上是说这些关键字中的每一个都在枚举范围内获得以下赋值:

int REG0 = 0;
int REGA = 1;
int REGB = 2;
int REGC = 3;
int REGD = 4;

This means that bus[REG0] is equivalent to bus[0]. The issue you're having, then, is mostly likely related to trying to fetch an element of bus that you never declared (which has an undefined value provided that bus is a local variable) or inadvertently changing some element of bus somewhere else in your code (which may give an unexpected result).

这意味着总线[REG0]相当于总线[0]。那么,您遇到的问题很可能与尝试获取您从未声明的总线元素(如果总线是局部变量而具有未定义的值)或无意中更改了您的其他位置的某个总线元素有关。代码(可能会产生意外结果)。

#1


0  

Have you initialized the array yet? Just writing short bus[5]; will allocate the memory for 5 short variables but that's all.

你有没有初始化阵列?只写短巴士[5];将为5个短变量分配内存,但这就是全部。

What you can do is short bus[5] = {0}; and it will initialize all entries to 0 so you know exactly what you're dealing with ahead of time.

您可以做的是短总线[5] = {0};它会将所有条目初始化为0,这样您就可以确切地知道您正在处理的是什么。

#2


2  

It's hard to tell because you haven't included any relevant code. But here are some ways you could get a wrong value:

很难说,因为您没有包含任何相关代码。但是这里有一些方法可以得到错误的值:

  1. You use a pointer of a wrong size and read past the end of the array:

    您使用大小错误的指针并读取数组的末尾:

    long* b = (long*)bus;
    result = b[4]; // this will read past the end of the array
    
  2. You copy from other location into the bus array, but use a wrong count:

    您从其他位置复制到总线阵列,但使用错误的计数:

    short bus[5];
    
    // this is 5
    int count = sizeof(bus)/sizeof(bus[0]);
    
    // this will copy 5 bytes, instead of 5 shorts
    // (this is why bus[10] would fix the issue, for example)
    memcpy(bus, some_location, count);
    
  3. You never initialize the array, but read the value before writing to it:

    您永远不会初始化数组,但在写入之前读取该值:

    // this *doesn't* clear the contents of the array
    short bus[5];
    
    // value of result is undefined
    result = bus[4];
    

#3


0  

To clarify: When you declare enum bus_sigs { REG0, REGA, REGB, REGC, REGD }; you are basically saying that each of these keywords gets the following assignment within the enum's scope:

澄清一下:当你声明enum bus_sigs {REG0,REGA,REGB,REGC,REGD}时;您基本上是说这些关键字中的每一个都在枚举范围内获得以下赋值:

int REG0 = 0;
int REGA = 1;
int REGB = 2;
int REGC = 3;
int REGD = 4;

This means that bus[REG0] is equivalent to bus[0]. The issue you're having, then, is mostly likely related to trying to fetch an element of bus that you never declared (which has an undefined value provided that bus is a local variable) or inadvertently changing some element of bus somewhere else in your code (which may give an unexpected result).

这意味着总线[REG0]相当于总线[0]。那么,您遇到的问题很可能与尝试获取您从未声明的总线元素(如果总线是局部变量而具有未定义的值)或无意中更改了您的其他位置的某个总线元素有关。代码(可能会产生意外结果)。