I’d like to add that the most common place you’d see this is when you’d mess up a recursive function or have some circular function. Stacks are last in first out data structures and it’s the structure of choice for storing the currently running function.
The Stack is typically the memory used to run your function. If your function calls another function then you push the memory from the called function onto “the stack”. When you return from a function it discards the current stack and loads whatever is at the top of the stack.
So for example in a web app, you’d have a stack that would look like a routing function, calling a function on a controller class, calling a function on a service, that calls a function on a repository, that calls a function on a ORM, that calls a function on a database connection object, that calls a function that sends SQL off to a server. Each function has a place on the stack where it’s state is remembered for when the currently executing function returns.
If you use VSCode and set a debugging breakpoint you can inspect the “Call Stack”, it should be the second to the bottom tab in the debug window. Clicking on the stack should alter which locals you see in the “Variables” tab.
A stack overflow most commonly happens when you run out of memory and can’t put anything else on the call stack.
Latest Answers