Program size increases when removing code

Hello guys.

I have got a strange problem which I don't understand. In my program I have two functions which I use to store and retrieve parameters from the eeprom memory, so far so good. The funny thing is that when I change the offset from 200 to 0 the program size increases by a 644 bytes :o . The same thing happens when I completely remove the address offset functionality from the program. How is this possible? Something wrong in the compiler? I'm using arduino IDE 1.8.4.

#define ADDR_OFFSET 200 //Offset of the eeprom adresses (program size increases when changed to 0!)

void storeParameter(int address, int value) {
  EEPROM.put(ADDR_OFFSET + address, value);

}

int retrieveParameter(int address) {
  int value;
  EEPROM.get(ADDR_OFFSET + address, value);
  return value;
}

Hard to tell from that snippet what the compiler is doing with the sketch.

It's likely that before the change there was some code that was optimized out and the change got it included.

Please post a complete program, not just some functions. I compiled below with IDE 1.8.5 for a Leonardo and don't see the behaviour.

#define ADDR_OFFSET 0 //Offset of the eeprom adresses (program size increases when changed to 0!)
/*
200:
Sketch uses 4176 bytes (14%) of program storage space. Maximum is 28672 bytes.
Global variables use 151 bytes (5%) of dynamic memory, leaving 2409 bytes for local variables. Maximum is 2560 bytes.

0:
Sketch uses 4176 bytes (14%) of program storage space. Maximum is 28672 bytes.
Global variables use 151 bytes (5%) of dynamic memory, leaving 2409 bytes for local variables. Maximum is 2560 bytes.
*/
void setup()
{
  int x = retrieveParameter(100);
  Serial.println(x);
}

void loop()
{
  storeParameter(100, 1234);
  int x = retrieveParameter(100);
  Serial.println(x);
}

void storeParameter(int address, int value) {
  EEPROM.put(ADDR_OFFSET + address, value);

}

int retrieveParameter(int address) {
  int value;
  EEPROM.get(ADDR_OFFSET + address, value);
  return value;
}

I ONLY change the value of "#define ADDR_OFFSET" 200 to 0. I cannot understand how that effects program size at all. I'm not changing anything else.

I will strip down the program to a smaller size (now about 31kb) and see if the problem still occurs. If I can't find the problem I will post the complete code.

Bass99: If I can't find the problem I will post the complete code.

Is it a problem? Are you running out of code space on whatever board you're using? Don't expend too much effort solving problems that you don't have.

gfvalvo: Is it a problem? Are you running out of code space on whatever board you're using? Don't expend too much effort solving problems that you don't have.

Isn't it a problem when you don't know why your code does what it does? I know it'd bother me!

GoForSmoke: Isn't it a problem when you don't know why your code does what it does? I know it'd bother me!

Yes, but I thought this was a case of the compiler doing something that's not understood. Getting to the bottom of that may require a much broader and deeper knowledge of things.

If I get results like that from the compiler, I would be sure that my code isn’t right. Where else did I use that define?

I think that I would start by changing the #define to a const variable.

Hi Guys,

I found some time to investigate the problem but still no solution. I stripped down my sketch to see what is causing the issue but without succes. In the attachement you will find a sketch which illustrates the problem. When you change the #define ADDR_OFFSET from 2 to 0 the program size increases :o

What is causing this?

Kind regards,

Bas

EEPROM.ino (2.22 KB)

sketch_oct22a.ino (1.15 KB)

GoForSmoke: I think that I would start by changing the #define to a const variable.

Did you try this?

Pieter

I just tried "const unsigned ADDR_OFFSET = ' on an UNO and got the same anomaly: 1104 bytes of program memory with offset 2 and 1194 bytes with offset 0.

I changed EEPROM.ino to EEPROM.cpp and moved some things around to get it to compile. The byte counts then became 1086(2) and 1194(0), an even larger difference. The EEPROM.cpp.o files are 17024(2) and 17772(0) bytes with no change in the sketch .o file size. Looks like all of the differences are in the EEPROM file, as one would expect since it is the only place where ADDR_OFFSET is used.

The next step would be to dump the .elf file in assembler and look for differences.

Thanks for trying john.

Funny thing is when you completely remove the offset form the code the program size is also 1194. So adding code makes size smaller... :o In my original program the difference is about 500 bytes!

For now is not really a big problem since I have just enough space left, but in the future this can be a problem. So if anybody knows what is going on please let me no.

Cheers

Is it possible the compiler is inlining the function when you are adding the offset of 0, but keeping it as a function with the offset of 2?

I notice you get the same code size difference if you remove the #define and instead directly use the numbers.

(edit) I did a bit of testing, if I replace the calls to the storeParameter() function with the equivalent inline code, then the code size decreases to 1192 bytes when the #define = 0, and increases size to 1212 bytes when #define = 2, which would be consistent with the compiler inlining the function when it sees the addition of 0 is unnecessary, but keeping it as a function call when adding 2. The retrieveParameter() function appears to be compiling as expected regardless of the value of the #define.