2

Somewhere I read that if I never return from main() loop, I can spare ~66 Bytes with some compiler switch in avr-gcc, but I couldn't find the site anymore.

This is for embedded:

main() {
 while(1)
 {
   // do stuff
 }
}
Farzad
  • 842
  • 2
  • 9
  • 26
Vincent Alex
  • 324
  • 3
  • 12

3 Answers3

5

For gcc you can use a special attribute to indicate that your function does not return:

int main() __attribute__ ((noreturn)) {
    for (;;) {
         // do stuff
    }
    __builtin_unreachable ();
}

Optionally you can add __builtin_unreachable (); to indicate that some part of the code can never be reached.

Although in most cases properly recognised by optimisation flags, without such while(1) can generate more code than for(;;).

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • I was hopping for a non-compiler specific code part (like a comp. flag), but it will do, thanks! – Vincent Alex Apr 14 '14 at 18:21
  • Interesting while(1) vs for(;;) turns out to be the same asm: http://stackoverflow.com/questions/885908/while-1-vs-for-is-there-a-speed-difference – Vincent Alex Apr 14 '14 at 18:24
0

I think these post are what you are looking for: http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=60775

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=107551&start=0

  • [Sergey L](http://stackoverflow.com/users/1700513/sergey-l) just pinned an exact point You're linking to. Bare URL isn't the best possible answer ;} – Kamiccolo Apr 14 '14 at 15:55
0

Found it! If you are sure your main routine will never exit, you can use the "-mendup-at=main" flag when compiling. This will save 6 bytes of ROM.

Vincent Alex
  • 324
  • 3
  • 12