Tuesday, June 28, 2011

Finding Stack Direction for a Machine

How would you find out if a machine’s stack grows up or down in memory?

You need to call a function with one parameter, and create one more local variable, the parameters would be pushed on stack and also the local variables would be created on stack, hence, if your parameters address and locals address would give you the result.

Here is the code of subroutine for your reference.

void sub_routine(int a) { 
int b;
b = a;
if(&b < &a)
printf("Stack Grows Downward!");
else
printf("Stack Grows Upward!");
}

int main() {
sub_routine(10);
return 0;
}

No comments:

Post a Comment