Search code examples
cstackmicrocontrolleravravr-gcc

Why does avr-gcc bother to save the register state when calling main()?


The main() function in an avr-gcc program saves the register state on the stack, but when the runtime calls it I understand on a microcontroller there isn't anything to return to. Is this a waste of RAM? How can this state saving be prevented?


Solution

  • Most likely main is just compiled in the same was as a standard function. In C it pretty much needs to be because you might call it from somewhere.

    Note that in C++ it's illegal to call main recursively so a c++ compiler might be able to optimize this more. But in C as your question stated it's legal (if a bad idea) to call main recursively so it needs to be compiled in the same way as any other function.