I've encountered something odd which I don't understand in this code:
#include <EEPROM.h>
struct Settings {
char stringy[24] = "initial";
};
void setup() {
Serial.begin(74880);
EEPROM.begin(100);
Settings s;
strncpy(s.stringy, "a string", sizeof(s.stringy));
EEPROM.put(0, s);
Settings s3 = readSettings();
Serial.println(s3.stringy);
}
Settings& readSettings() {
Settings s2;
Serial.println(s2.stringy);
EEPROM.get(0, s2);
delay(1);
return s2;
}
void loop()
{
}
EEPROM implementation is this: https://github.com/esp8266/Arduino/tree/master/libraries/EEPROM
The code as it is prints:
initial
a string
But if I comment the delay(1)
, then it prints
initial
initial
What? The delay(1)
can be replaced with seemingly an any other statement.
What's going on here? Where is this strange async-seeming behaviour coming from?
If I make readSettings
return a Settings
object instead of Settings&
then the issue also goes away.
I'm confused, what am I missing here?