Pointers and Dynamic Memory – Stack and StackOverflow

From the article Pointers and Dynamic Memory – Part1, I made a little introduction of Pointers and a brief overview of Dynamic memory. In this article, we will get into some more details of Stack and StackOverflow in the context of Pointers and Dynamic memory.

Let’s see a simple C program which we can use to see what happens in memory when it executes

#include<stdio.h>
 int total;
 int square(int x)
 {
     return x*x;
 }
 int SquareSum( int x, int y)
 {
      int z = square(x+y);
      return z;
 }
 int main()
 {
      int a=10, int b=20;
      total = SquareSum(a,b);
      printf(“Output = %d”, total);
 }

From the program, we can see that there is a function that gives the Square of a number and another function that gives the square of the sum. In the Main method, the square of the sum is passed through the arguments a and b.

Like I mentioned in the previous article, Stack is reserved for function calls and local variables and Static means the global variables that are not stored inside the variables. From the C code, this can be interpreted under Stack and Static.

  1. When the main method is invoked, some amount of the memory is allocated to the Stack for execution of main() as described in the Yellow box.
  2. The total is a global variable so it gets into the Static section.
  3. All local variables, arguments and the information where the function main should return are stored within the stack frame. The size of the stack frame for a method is calculated when the program is compiling.
  4. Now, when main calls SquareSum method, then a stack frame is allocated for the call SquareSum.
  5. All the variables x, y, and z will sit in the same stack frame.
  6. The function sum of the square that is “square” will also be found in the Stack frame and it will have its own local variables.

When the program executes, the function at the top of the stack keeps on executing the rest are in a “pause mode” waiting for other function to return something before it resumes execution. When the whole program finishes executing, the stack and static frame get cleared. Remember, a global variable should only be set with the aim to use it for a whole lifetime of the program, otherwise, it will be a waste of memory. The global variable used in this program has been done deliberately to understand the concept.

Another interesting issue is that when the program starts, by default, the operating system will reserve some amount of space on the stack. When there are functions in a stack calling other functions until there is no memory available in the stack, its called “StackOverflow” which consequently crashes the whole program. For example, a bad program having bad recursion codes. In brief, there are limitations to stack.

Tagged
Nitin J Mutkawoa https://tunnelix.com

Blogger at tunnelix.com | Founding member of cyberstorm.mu | An Aficionado Journey in Opensource & Linux – And now It's a NASDAQ touch!

You May Also Like

More From Author