Pages: [1]   Go Down
Author Topic: [False asumption, sorry!] Unused functions are compiled, how to avoid this?  (Read 1058 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello smiley

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++ ?
« Last Edit: October 28, 2012, 11:53:50 pm by guix » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: October 28, 2012, 10:57:34 pm by lloyddean » Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edited my response above adding some explanation.
Logged

SE USA
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3783
@ssh0le
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 510
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13072
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I wasn't sure...

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

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 510
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
void setup ()
  {
  }
void loop () {}

Gives:

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



Add a large array:

Code:

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

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

Gives:

Code:
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:
byte foo [1000] = { 1, 2, 3, 4 };

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

Gives:

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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 510
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Yeah, check this out:

Code:
void setup()
{
}

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

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

Gives:

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

Same as the empty sketch.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

France
Offline Offline
Edison Member
*
Karma: 38
Posts: 1012
Scientia potentia est.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

And for me, the exact same (empty) sketch gives 664 bytes, not sure why, maybe because I'm using an Arduino Mega 2560?
« Last Edit: October 28, 2012, 11:54:49 pm by guix » Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 510
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 510
Posts: 19306
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I get:

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

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

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Pages: [1]   Go Up
Jump to: