CPU uses stack to pass parameters and to save return address before jumping to next function call. It also uses stack memory for loading and storing local variables. Below is a stack grow example of a function call which has this following prototype-

int function (int argument1, int arrgument2)
{
  int local;
  ....
  
}
Stack Grows in UP or Down

Here we have a local variable Local. Program calls more and more inner functions, call frame grows. With the above prototype compiler pushes argument2 then argument1 and then call the function. Call instruction pushes the return address and further inside the function compiler pushes BP. Compiler will put local after this. Now as stack grows in a perticuler direction so after each and every inner function call, address of the local would be either increasing or decreasing position. If we compare the address values we would definitely get the direction details. Here is a small C code example. Here we are passing the address of a local of an outer function to an inner function call. Now as we called a function stack has grown. Next is compare the address of two locals.

int dummy_call(void * p)
{
  unsigned int local;
  if (&local < p) {
    return -1;
  } else {
    return 1;
  }
10  }
11  int stack_grows(void)
12  {
13    unsigned int local;
14    return dummy_call(&local);
15  }
16  int main(int argc, char* argv[])
17  {
18    printf("Stack grows in %s direction\n", stack_grows() < 0 ? "Lower" : "Higher");
19 
20  }
21 
22 
23  Output:
24  Stack grows in Lower direction.
25 

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

trace a memory leak, detect memory corruption, Big Endian and Little Endian, stack grows up or down, C and Data Structures, Data Structures using array,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan