Simple explanation on stack overflow
Some time ago, I was briefly explaining some abstract data structures (Stacks, queues, linked list, etc) to my mentee. I brought up the popular coding resources website “stackoverflow” and I realized that even though the site was frequent by a lot of web/mobile developers, many of them do not really appreciate what the term actually means.
What is stack?
To understand stack overflow, we must first understand what stack is.
The stack is a special area of computer’s memory set aside for a thread in execution. When a function is being called, a block is reserved on the top of the stack for storing the return address, parameters and local variables. When the function returns, the memory of the variables will be automatically erased.
The stack is always reserved in a LIFO (last in first out) order, with PUSH (adds element to collection) and POP (removes the most recently added element) as the two main operations.
What is heap?
The heap is memory set aside for dynamic allocation. Unlike stack, it does not have a specific limit on memory size. Memory is dynamically allocated and explicitly deallocated at run-time.
What is stack overflow?
Stack overflow is a run-time error that happens when there is not enough memory available in the thread’s stack.
For example, the below code snippet declares a very large integer array (in stack):
When running it in debug mode, there will be a “stack overflow” run-time error:
Now, to resolve the issue, one way would be to dynamically allocate memory in the heap instead:
Another way is to expand the stack memory size of your application inside your IDE (in this case, I am using Microsoft Visual Studio):
And the program is able to execute properly:
So this is the concept of stack overflow explained using simple C++ codes.
I have always advocated that theory and practical come hand-in-hand. Being able to associate the actual implementation with the theory behind it will solidify your understanding and allows you to go further in your field of expertise.
Bibliography
About the Author
Dennis Chang is a seasoned entrepreneur, solution architect and CTO. He is currently the Chief Technology Officer (CTO) of software house El Pando Pte Ltd and healthtech firm Carer Pte Ltd.
Dennis started his professional career in the smart card and cybersecurity sector, and moved on to the military software industry, where he led his team in the development of multi-million-dollars Real-Time Distributed Naval Warfare Instrumentation System.
Dennis founded multiple technology companies between 2005 and 2017. He was also engaged as the CTO for various tech firms. The various software systems designed by Dennis were exhibited in international IT conferences like Info-security Asia, IMDEX Asia, Tech In Asia, as well as local events like Lifelong Learning Festival and WDA Skills Future Roadshow.
Dennis has a Master of Science degree in Communication Software and Networks, as well as a Bachelor of Engineering (EEE) (Hons) degree from Nanyang Technological University (NTU) of Singapore. His research interest is in the area of Software Design and Artificial Intelligence.