GeeksforGeeks » Interview Questions

Stack is growing upwards or downwards?

(4 posts)

Tags:

  1. kp101090
    Member
    Posted 1 year ago #

    To check whether processor stack is growing upwards or downwards !!!

  2. Murli
    Member
    Posted 1 year ago #

    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");
       }
       else
       {
         printf("\nStack is growing upwards.\n");
       }
       printf("\n\n");
    }
    
  3. required
    guest
    Posted 1 year ago #

    int main(){
      int a;
      int b;
      if(&a <&b)
        printf("growing up");
      else
        printf("growing down");
      getchar();
      return 0;
    }
    

    ANS: growing down

  4. Shashank Mani Narayan
    guest
    Posted 1 year ago #

    void  Up_down(int i)
    {
       int ptr=&i;
    
       printf("%u  \n",ptr);
       up_down(j);
    }
    
    int main()
    {
         Up_down(10);
         getchar();
         return 0;
    }
    

    so By looking at the address of ptr u can say whether stack is growing up or down


Reply

You must log in to post.

RSS feed for this topic