Go Down

Topic: [False asumption, sorry!] Unused functions are compiled, how to avoid this? (Read 1 time) previous topic - next topic

guix

Hello :)

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++ ?

lloyddean

#1
Oct 29, 2012, 04:52 am Last Edit: Oct 29, 2012, 04:57 am by lloyddean Reason: 1
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.

lloyddean

Edited my response above adding some explanation.

Osgeld

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
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

Nick Gammon


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.
http://www.gammon.com.au/electronics

Coding Badly

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).

guix

Ok I wasn't sure...

This is example code:
Code: [Select]

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: http://www.arduino.cc/en/Hacking/BuildProcess , I realized I was wrong, the .cpp.o is not what is uploaded to the Arduino :)

Nick Gammon

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

Code: [Select]

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


Gives:

Code: [Select]

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





Add a large array:

Code: [Select]


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

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


Gives:

Code: [Select]

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:

Code: [Select]

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

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


Gives:

Code: [Select]

Binary sketch size: 1,316 bytes (of a 2,048 byte maximum)
http://www.gammon.com.au/electronics

Nick Gammon


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


Yeah, check this out:

Code: [Select]

void setup()
{
}

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

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


Gives:

Code: [Select]

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


Same as the empty sketch.
http://www.gammon.com.au/electronics

guix

#9
Oct 29, 2012, 05:52 am Last Edit: Oct 29, 2012, 05:54 am by guix Reason: 1
Yes, I wasn't sure if the size reported was correct, I thought maybe it was an estimation :)

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

Nick Gammon

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.
http://www.gammon.com.au/electronics

Nick Gammon

I get:

Code: [Select]

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


Which is rather ominous I have to say, with Halloween around the corner.
http://www.gammon.com.au/electronics

Go Up