Bloomberg LP Interview Question
Financial Software DevelopersNeed to note that if you define two integers in main() and print their addresses out, you may think it could give you some idea that how stack grows but actually it can't, cauz that program may be optimized by the compiler so that the definition order may not be the execution order. You just cannot trust the result.
i was searching why people never did that .. this seems to be a good reason :) .. anyone has any other opinion
one can easily determine the stack direction by declaring one integer in main and other one in a function , called in main.
As far as "which is Better? " question is considered, I don't think it really matters....They both are good as they both move towards center of memory allocated....
Stack growing UP is better to avoid stack overflow issues. If stack grows down, and you use insecure input function like strcpy, scanf and others.. malicious users can input large string and that would overflow the buffer. Since in C there is no bounds checking, it can result in overwriting of the EBP and the saved EIP. This can happen only if stack grows down. If it grows up, the overflow occurs in yet to be written area of the stack.
here s what i did..
main(){
int a;
printf("addr of main=%u",main);
printf("addr of a=%u",&a);
sub();
}
sub(){
int b;
printf("addr of sub=%u",sub);
printf("addr of b=%u",&b);
}
..in the result i got addr of main < addr of sub .. but addr of a > addr of b..
Could anyone explain this or where im wrong ??
Thanks in advance.
}
- Anonymous June 03, 2010