Web Toolbar by Wibiya

Pages

Friday, April 13, 2012

Determine direction of stack growth.

Solution #1: The direction of stack growth depends on OS as well as on the architecture. Below is a program to detect the direction of stack growth,

void b(int* x)
{
    int y;

    //if address of y is less than address of x
    //then the direction is downwards, upwards otherwise
    if(&y < x)
        printf("Stack growing downwards.\n");
    else
        printf("Stack growing upwards.\n");
}

void a()
{
    int x;
    b(&x);
}

int main()
{
    a();
    return 0;
}