Im taking an assembly class and we're currently learning about the stack. From what I've learned everything that's pushed into the stack must eventually be popped (cleaning up the stack). Why do we absolutely need to clean up the stack? I've read it is to prevent memory leaks, is that it?
Thank you very much
There is only a certain amount of stack for a given thread of execution.
Its purpose is to temporarily hold data needed when a function is called (such as the return address and parameters passed to the function).
If you do not clean up the stack when your function exits, you will eventually run out of stack space.
Also, as a general rule that goes beyond just the stack, any resource your program uses (stack space, heap space, file handle, etc.) should be held for the shortest possible amount of time to improve overall efficiency.
The stack is a finite amount of memory, and as with any memory allocation system if you never clean it, it'll simply grow and grow. Eventually you'll overflow the stack and all hell breaks loose as you overwrite other areas of memory or simply generate an invalid address.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With