Storing STRUCT in EEPROM?

I have 3 bytes to store in the EEPROM and I could do it like

EEPROM.put(0, b1);
EEPROM.put(2, b2);
EEPROM.put(3, b3);

and then it will ONLY be RE-stored if it in fact DIFFERS from the value allready stored.

Is that the case too if the 3 bytes are wrapped into a struct and only - let's say - 1 of them has a new value - something like this:

struct myStruct {
b1;
b2;
b3;
}

EEPROM.put(0, myStruct);

AFAIK, the EEPROM library can store any data type, including a struct. The standard EEPROM library performs an update, not a blind write. It will only write information that has changed.

Although it's a good feature, you shouldn't write code that depends on it.

Ha, ha, the examples show it CAN handle struct but my Q was about the members of the struct.

If ONE has a new value will ONLY this single ONE be RE-saved and the other members NOT - just to NOT eat up of the 100.000 times the EEPROM can be written to ?

Have you looked at the library source code? What about the reference page for put()? :slight_smile:

"This function uses EEPROM.update() to perform the write, so does not rewrites the value if it didn't change."

It would not take much effort to write a small sketch to find out for yourself. Experimenting is allowed if not encouraged.

1 Like

Yeah, that's why I asking - the STRUCT IS changed if just one of its members has changed - so the STRUCTURE will be RE-saved

OR is it the individual members that is evaluated for changes - that's my Q ?

Yeah, that would only take 100.000 or more put's - no big deal at all ... or ... ? :thinking:

The eeprom is written byte by byte and .update() is also on a byte basis. Each byte is evaluated for change from what is stored at the address and only written if different.

Source code of writing the struct byte by byte with a 'update' for each individual byte: https://github.com/arduino/ArduinoCore-avr/blob/master/libraries/EEPROM/src/EEPROM.h#L140

Not stuff for a novice like me

That's my answer - thx !

Have you verified what has been told in Post-8?

The novice I am I can't manage the cpp code for the EEPROM.

It's working now with get and put a struct and 100.000 times is way enough in my life time.

@ GolamMostafa
How would you verify that an unchanged byte in a struct is indeed unwritten and not written again with the same value?

Another posting shows the cpp-file for the EEPROM but it's out of my reach to really get something out of it ...

I will try to find if some means does exist.

It does exist. Just read the cell and compare with the value that you want to store; if it differs, write the new value, else do nothing.

That's the principle used in EEPROM.h (ArduinoCore-avr/EEPROM.h at master · arduino/ArduinoCore-avr · GitHub) as far as this untrained C++ programmer can see.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.