[False asumption, sorry!] Unused functions are compiled, how to avoid this?

Hello :slight_smile:

Just a simple question, I noticed if a function is not used anywhere (never called), it is still compiled. In another language that I use frequently (PAWN), there is the prefix keyword "stock", that tells the compiler "if the function is not used anywhere, do not include it".

How to do the same thing in C++ ?

With the Arduino IDE this is automatic as long as the function is never referenced by your code it will be stripped from the code uploaded to your Arduino.

EDIT: The IDE will compile your code with a 'compiler' converting it to 'assembler' source which is passed to the 'assembler' which outputs 'object code' which is passed to the 'linker' which links your code with the Arduino library. The 'linker' will strip any code that your 'sketch' does not reference directly or indirectly.

Edited my response above adding some explanation.

A while back a gang of us was trying to get complile sizes down to minimal as a pissing contest, the difference in that example code from 1.0 and 0.22 was over 20 bytes with nothing else changed

so it seems that not everything is stripped if you don't reference it, though I dont think its really functions, its more like features added to functions

guix:
Just a simple question, I noticed if a function is not used anywhere (never called), it is still compiled. In another language that I use frequently (PAWN), there is the prefix keyword "stock", that tells the compiler "if the function is not used anywhere, do not include it".

Example code please, and proof this happens.

lloyddean:
The 'linker' will strip any code that your 'sketch' does not reference directly or indirectly.

A little pedanticy ... The compiler can also excise unused code (and data).

Ok I wasn't sure...

This is example code:

void setup()
{
}

void loop()
{
  //func();
}

void func()
{
  char str[256];
  sprintf(str, "blablablablablabla");
}

I clicked Verify, then looked the resulting .cpp.o file in IDA and saw that "func" was still present.

But after lloyddean's answer, and looking this page: Redirecting , I realized I was wrong, the .cpp.o is not what is uploaded to the Arduino :slight_smile:

The compiler's pretty aggressive about removing stuff. For example:

void setup () 
  { 
  }
void loop () {}

Gives:

Binary sketch size: 310 bytes (of a 2,048 byte maximum)

Add a large array:

byte foo [1000] = { 1, 2, 3, 4 };

void setup () 
  { 
  }
void loop () {}

Gives:

Binary sketch size: 310 bytes (of a 2,048 byte maximum)

No change.


Refer to something in the array, and not only does it generate code to do that, it includes the array in the object:

byte foo [1000] = { 1, 2, 3, 4 };

void setup () 
  { 
  foo [0] = 1; 
  }
void loop () {}

Gives:

Binary sketch size: 1,316 bytes (of a 2,048 byte maximum)

guix:
I clicked Verify, then looked the resulting .cpp.o file in IDA and saw that "func" was still present.

Yeah, check this out:

void setup()
{
}

void loop()
{
  //func();
}

void func()
{
  char str[256];
  sprintf(str, "blablablablablabla");
}

Gives:

Binary sketch size: 310 bytes (of a 2,048 byte maximum)

Same as the empty sketch.

Yes, I wasn't sure if the size reported was correct, I thought maybe it was an estimation :slight_smile:

And for me, the exact same (empty) sketch gives 664 bytes, not sure why, maybe because I'm using an Arduino Mega 2560?

Ah yes, well the Hardware Serial implementation on the Mega2560 instantiates 4 serial objects even if you don't want them. I suspect they stay there because they are initialized in init().

There may be other reasons as well.

I get:

Binary sketch size: 666 bytes (of a 258,048 byte maximum)

Which is rather ominous I have to say, with Halloween around the corner.

I tryed an empty sketch on a DUE.
22082 bytes.
And when disassembling there where a lot about serial.