简体   繁体   English

为什么我不能使用函数计算的 int 作为索引访问该数组中的值?

[英]Why can't I access a value from this array using a functionally calculated int as an index?

EDIT: I mistakenly assumed I should ask this as a general C++ question, and realized later that this issue came from my usage of C++ within the Arduino environment.编辑:我错误地认为我应该把这个作为一个通用的 C++ 问题来问,后来意识到这个问题来自我在 Arduino 环境中使用 C++。 In my example, I didn't include my usage of an Arduino modifer PROGMEM on the array which moved its storage from RAM to FLASH and is ultimately what caused the calculation issue.在我的示例中,我没有在阵列上使用 Arduino 修改器PROGMEM ,该阵列将其存储从 RAM 移动到 FLASH,最终导致计算问题。 Accessing the PROGMEM array required me to use an accessor method pgm_read_byte instead of an index.访问 PROGMEM 数组需要我使用访问器方法pgm_read_byte而不是索引。

I have an array:我有一个数组:

const PROGMEM uint8_t daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

I have a functionally calculated uint8 from a time library:我有一个来自时间库的功能计算 uint8 :

uint8_t month = tm.month();

I have a size_t:我有一个 size_t:

size_t monthIndex = (size_t)month;

I've tried using either month or monthIndex to access a value from this array, however the same result happens:我尝试使用monthmonth monthIndex来访问该数组中的值,但是发生了相同的结果:

# For size_t monthIndex = 1 or uint8 month = 1; ...
uint8_t currentDaysInMonth = daysInMonth[monthIndex];

# >> Expected = 28;
# >> What I actually get = 61;

How do I get the expected array value?如何获得预期的数组值?

Where is 61 coming from? 61从哪里来?

EDITS编辑

This is the relevant portion of the DateTime class for tm这是tm的 DateTime 类的相关部分

class DateTime {
public:
  DateTime (uint16_t year, uint8_t month, uint8_t day,
              uint8_t hour = 0, uint8_t min = 0, uint8_t sec = 0);

  uint8_t month() const       { return m; }
}

Datetime constructor日期时间构造函数

DateTime::DateTime (uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec) {
    if (year >= 2000)
        year -= 2000;
    yOff = year;
    m = month;
    d = day;
    hh = hour;
    mm = min;
    ss = sec;
}

Minimal example:最小的例子:

DateTime tm = DateTime(2020, 2, 1, 0, 0, 0);

const PROGMEM uint8_t daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

uint8_t month = tm.month();

uint8_t currentDaysInMonth = daysInMonth[month];

>> currentDaysInMonth Returns 61
>> expect 31 from this example, though would offset index - 1 once I figure out what the issue is.

Print logs:打印日志:

std::cout << month << std::endl;
std::cout << tm.month() << std::endl;
std::cout << currentDaysInMonth << std::endl;

# >> 2
# >> 2
# >> 219 -- sometimes its 61, 45, 219, not sure the rhyme or reason to this value

I mistakenly assumed I should ask this as a general C++ question, and realized later that this issue came from my usage of C++ within the Arduino environment.我错误地认为我应该把这个作为一个通用的 C++ 问题来问,后来意识到这个问题来自我在 Arduino 环境中使用 C++。 In my example, I didn't include my usage of an Arduino modifer PROGMEM on the array which moved its storage from RAM to FLASH and is ultimately what caused the calculation issue.在我的示例中,我没有在阵列上使用 Arduino 修改器 PROGMEM,该阵列将其存储从 RAM 移动到 FLASH,最终导致计算问题。 Accessing the PROGMEM array required me to use an accessor method pgm_read_byte instead of an index.访问 PROGMEM 数组需要我使用访问器方法 pgm_read_byte 而不是索引。

Solution:解决方案:

DateTime tm = DateTime(2020, 2, 1, 0, 0, 0);

const PROGMEM uint8_t daysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

uint8_t month = tm.month();

# use the pgm_read_byte accessor
uint8_t currentDaysInMonth = pgm_read_byte(&daysInMonth[month - 1]); 

Thanks everyone for your help!感谢大家的帮助! Sorry for the initial lack of information.抱歉最初缺乏信息。

Some resources:一些资源:

https://www.arduino.cc/reference/tr/language/variables/utilities/progmem/ https://www.arduino.cc/reference/tr/language/variables/utilities/progmem/

https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html https://arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM