ctestingpicxc8

Is a stack overflow on a compiled stack really impossible?


When I have finished a software, I carry out a stack overflow test at the end. Why don't I have to do this with a compiled stack?
Are overflows really completely impossible here or only very rare?


Solution

  • The XC8 "compiled stack" isn't really a stack at all, in the sense of pushing and popping. It holds the kind of data that would traditionally be allocated "on the stack" - local variables - but it doesn't grow. There's no stack pointer. Everything is just allocated at a fixed address. The "stack" can't grow past its limits if it doesn't grow in the first place.

    This of course runs into other limitations not found with traditional stack allocation. For example, functions that use the compiled stack can't be called recursively. All calls to such functions have their local variables allocated at the same addresses, so if you tried to make a recursive call, it'd be stomping over the original call's local variables.

    You can read more in the XC8 C Compiler User's Guide, particularly section 5.5.2.2.1, "Compiled Stack Operation".