I read different responses to the question of detecting stack growth detection and I understand that, in modern architectures, stack might grow randomly, might be created off heap, and so on.
However, in this classic interview question, I want to understand why people use a function call rather than comparing 2 local variables in the same function. I think there must be some particular reason for doing this but, not being a C/low level developer [Java :)], I am simply guessing.
Here is the code I tried:
void sub (int *a) {
int b;
int c;
printf ("a:%d\n", a);
printf ("b:%d\n", &b);
printf ("c:%d\n", &c);
if (&b > a) {
printf ("Stack grows up.\n");
} else {
printf ("Stack grows down.\n");
}
}
int main (void) {
int a;
int b;
sub (&a);
printf ("\nHere we go again!!\n");
if (&b > &a) {
printf ("Stack grows up.\n");
} else {
printf ("Stack grows down.\n");
}
return 0;
}
I also found this article which tries to optimize the solution which I don't understand either: http://www.devx.com/tips/Tip/37412
P.S: From different responses to this and other threads, it seems like the question itself is flawed/irrelevant, as an interview question it probably re-enforces incorrect assumptions unless someone researches the answer !
Thanks!