Simple explanation on stack overflow
www.educba.com

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):

No alt text provided for this image

When running it in debug mode, there will be a “stack overflow” run-time error:

No alt text provided for this image

Now, to resolve the issue, one way would be to dynamically allocate memory in the heap instead:

No alt text provided for this image

Another way is to expand the stack memory size of your application inside your IDE (in this case, I am using Microsoft Visual Studio):

No alt text provided for this image

And the program is able to execute properly:

No alt text provided for this image

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

  1. https://en.wikipedia.org/wiki/Stack-based_memory_allocation
  2. https://en.wikipedia.org/wiki/C_dynamic_memory_allocation

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.

To view or add a comment, sign in

More articles by Dennis Chang

  • Threads and Multi-threaded Programming

    When I heard about the new social media app “Threads” of Meta, the first thing that came to my mind was, did some…

  • Introduction to Cryptography

    Cryptography involves two processes: Encryption and Decryption. Encryption is the process of transforming a plaintext…

    2 Comments
  • Exception handling in Object Oriented Language

    Exception Handling is a common concept in object oriented programming languages like C++, Java and C#. Exceptions occur…

  • Function and Operator Overloading in C++

    Function Overloading: Function Overloading is a programming concept whereby a function can be reused with different…

  • Pointers in C Language

    1. Introduction C is a very powerful programming language that are used by many software developers to develop…

  • Introduction to classes and objects in C++

    C++ had been the most dominant programming language in the 80s, 90s and even early 2000s. Till this day, many academic…

    1 Comment
  • Using DISC for Sales Profiling

    DISC is a behaviour assessment tool based on the DISC theory of psychologist William Marston. Marston’s theory centres…

  • What is Blue Ocean and how to find them?

    1. Introduction Blue ocean strategy is about creating uncontested market space by reconstructing market boundaries and…

  • Which to create: Facebook profile, group or page?

    Facebook is one of, if not the most, popular social media platform in the world. Apart from connecting with your…

    2 Comments

Explore content categories