Execution Stacks and when they Overflow
https://unsplash.com/@stephenjamesrheeder

Execution Stacks and when they Overflow

In computer science, a thread of execution is the smallest sequence of programmed instructions that can be managed independently by a scheduler, which is typically a part of the operating system. Normally, when you start your program, the compiler already breaks your program into independent threads which are passed to the runtime independently.

Each execution thread has a stack where local variables and arguments are allocated when a function or a method is invoked. A stack is used because it naturally follows the execution flow of method and function calls. The topmost record contains the data about the currently executing function; below that is the record of the caller of the function, which sits on top of another record of its caller, and so on.

No alt text provided for this image


In the example above, we have a function that calculates the factorial of a number n. Since this function is recursive, it does not compute the final result until the end condition n <= 1 is met. All preliminary result sets are held in memory and are contained in a stack that is bound to the currently executing thread.

e.g. the factorial of 5

No alt text provided for this image

In memory, our stack contains 5 variables, this is all good. A design-based question arises, how much information can this stack hold? We need to know this threshold to avoid the next error, the Stack Overflow exception.

Trying to calculate the factorial of n = 50000000 results in a Stack Overflow Exception - easily.

To view or add a comment, sign in

More articles by George Wanjohi

  • Mauritius: From Indentured Labour to a Smart Nation — A Journey of Growth, Unity, and Progress

    Photo by Teodor Kuduschiev on Unsplash As Mauritius prepares to celebrate Indentured Labourers’ Day on November 2nd…

  • The Power of Records Composition in Functional Programming

    Functional Programming (FP) emphasizes immutability, pure functions, and declarative coding. Among its powerful…

  • Solar for Africa

    Africa is the worlds second largest and second most populous continent after Asia in both cases. It is about 30.

  • Accounting: The great divide.

    Among other routines, managers are inherently accountable for most of their departmental/business finances. Provided…

  • Strings Revisited

    “Ten, nine, Ignition Sequence Start, six, five, four, three, two, one, zero. We have a light off! Lift off…”, Strings…

  • Memory Management at Runtime

    In computing, memory is a device or system that is used to store information for immediate use in a computer or related…

  • Why the Soviet Computer Failed

    "In 1986, the Soviet Union had slightly more than 10,000 computers. The Americans had 1.

  • Building a Reading Culture.

    This being the internet, the above post is no longer available. Did I not read the terms and conditions? The research…

  • Units of Measure

    Speed/ Velocity is calculated from distance and time, both of these are numerical figures. To avoid mixups, we have…

  • What is 0.1 + 0.7 in JavaScript?

    If your answer to 0.7 + 0.

Explore content categories