Silently breaking compiler standards by corrupting main() stack

I just spent about a day hunting down this largely-undocumented violation of compiler standards and unexpected behavior.

From the link above: “C/C++ guarantee that stack variables will exist for the duration of a function”. Violation of this guarantee should not be allowed unless done directly by the application developer.

Sorry you had a problem with this - and I note your point about documentation. It is done this way to recover the memory allocated to the stack for main() - which can be considerable and important on a small MCU. Our latest RISC-V ports do enable the user to decide if this should be done or not.

[on the point of breaching the C standard, this has been raised a few times and am not sure if it is the case or not. Maybe it is open to interpretation as once the scheduler has started the main() is no longer in context, so as per the standard variables in that block remain valid until “execution of that block ends in any way”]

I will also point out that it isn’t uncommon for small micro architectures to not fully implement the ‘requirements’ of high level languages, in the name of conserving precious resources. For instance, I have seen systems which just skip the zero initialization of global/file scope variables that are not explicitly initialized.