Thursday 2 August 2012

program to check if the stack grows up or down

Try noting down the address of a local variable. Call another function with a local variable declared in it and check the address of that local variable and compare!.


#include < stdio.h >
#include < stdlib.h >

void stack(int *local1);

int main()
{
int local1;
stack(&local1);
exit 0;
}

void stack(int *local1)
{
int local2;
printf("\nAddress of first local : [%u]", local1);
printf("\nAddress of second local : [%u]", &local2);
if(local1 > &local2)
{
printf("\nStack is growing downwards.\n");

printf " Stack is growing to Lower address" );
}
else
{
printf("\nStack is growing upwards.\n");

printf " Stack is growing to Higher address" );
}
printf("\n\n");
}

2 comments:

  1. This is really helpful! Thanks :)
    Please post more

    ReplyDelete
  2. Am posting more at my new site http://beacrazyprogrammer.com/
    do checkout

    ReplyDelete