ghostly function

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In my code (newest winavr, small project onto tiny2313, used compiling option for 90s2313) I created a function within main.c, but never used it. When I copiled it it gave one checksum. When i deleted the function, it gave a different checksum. Why? I NEVER USED THE FUNCTION, BUT IT HAS DONE SOMETHIN WITH THE CODE!?? How is this possible?

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I doesn't matter if it is used or not. It is included in the code. When you deleted it, the unused code was removed, hence a different checksum.

Randy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But thats wasting space! I have to get into 2k of memory! Cant i tell gcc somtething like "gcc -dont_give_unused_and_useless_things_into_the_code:)"?

There are pointy haired bald people.
Time flies when you have a bad prescaler selected.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not that I know of -- however, IMHO, thats your responsibility as a programmer if you are going to try to cram code into a small device. That or go to Assembler!.

Randy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Inside a given function, gcc will typically identify unreachable sections of code, and optimize them away.

However, it is not as easy to do that to get rid of an entire function. GCC compiles entire *.c files all at once, and then links the resulting *.o files together to produce the complete executable. All the optimization occurs at in the compiling satge (*.c -> *.o). By the time the linking stage happens (*.o + *.o + ... -> *.hex), it is too late to do any further optimization.

It is entirely possible that a given function will never be called within the *.c source file where it is written, but still be accessed by other functions in other linked *.c files. Because the compiler cannot know this (since linking has not happened yet) it cannot know for sure whether a particular function really is useless or not.

- Luke

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
However, it is not as easy to do that to get rid of an entire function.

Not that hard either. "Smart linkers" have been around for decades now...

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sure. But they are not included in the WinAVR distribution.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To delete an unused function from a .o file would require a very smart linker.

However the programmer can help himself by using the static keyword in conjunction with the function definition.

e.g. One of my functions in my file "anal.c"

static void UpdateRdgStatus (rdg_rec_t *rec, rdgstatus_t status, chan_t chan)

The static keyword means that UpdateRdgStatus is only used within "anal.c", the scope of the function is not global.

If UpdateRdgStatus is not used you will receive a warning to that effect.

A by-product is that the compiler will consider these functions for in-line coding and if succesfull produce smaller code.

Nigel