Eeprom get/put confusion

Probably a simple question but i can't find the answer anywhere.

Looking at the examples from these pages.

I can see how it writes and reads values from multiple positions using
eeAddress += sizeof(float);
eeAddress = sizeof(float);

But what happens when trying to read if the sketch changes the size of the first value in eeprom from say two bytes to one?
Does that leave an empty byte between address1 and address2?
How is this handled?

A running sketch can't change the size of a variable - that is fixed by the compiler.

What about something like this pseudo code

int firstAddress = 0;
int secondAddress = 0;

void setup() {
  EEPROM.get (firstAddress,2bytes);

secondAddress = sizeof(2bytes);

EEPROM.get (secondAddress,xbytes);


void loop() {

if (sensorvalue ==1023)
EEPROM.put (firstAddress,2bytes);
else {
    EEPROM.put (firstAddress,1byte);

Assuming the value in firstAddress is already 2 bytes. When the program sees the sensor value has changed, the value in firstAddress becomes 1 byte. secondAddress is still in the same position in eeprom but now there is an empty position in front of it.

firstAddress,secondAddress to firstAddress,empty,secondAddress

Does the program just ignore the empty position?

I don't understand - 1byte and 2bytes are not valid variable names.

A value of 1023 is still going to need two bytes of storage.

Yes. YOU are responsible for remembering where your data is. You are telling your sketch to store and fetch a byte from address 0. If you don't read or write address 1 then your sketch will ignore the address you no longer use.

However, if you change your setup to say secondAddress = sizeof(1byte);
the value of secondAddress will change and you will be getting that data item from the wrong address.

I think i follow now. So if secondAddress = sizeof(firstAddress) When the size of the firstAddress changes and you call the secondAddress it will skip the empty section in the eeprom

No, you don't follow at all.
sizeof(firstAddress) is a constant.
It can only change if you change the type of firstAddress and recompile the code.

Those particular lines of code should probably not be in an example, because they can be a bit confusing. sizeof(float) returns the number of bytes needed to store a float. A float always takes the same number of bytes (for a given arduino board) regardless of the value stored in the float.
eeAddress was originally set to 0, where a float was then stored, so the next unused location is eeAddress + (number of bytes needed for float). It is more conventional to hardcode the eeprom addresses as constants instead of dynamically calculating in the code, even easier to use a struct for all the data being stored in eeprom.

I thought they wouldn't be ideal examples. I already have hardcoded what I'm working on into the eeprom, was just trying to get my head around whether this was a more efficient method. I hadn't thought of putting all the data into a struct though thanks.

Yes so regardless of whether the size of firstAddress changes from 2 bytes to 1, the program will always look for secondAddress in the same position it was when it was first compiled

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