How will you find out if a stack grows up or down in a system? [duplicate]

依然范特西╮ 提交于 2019-12-25 01:23:55

问题


Possible Duplicates:
stack growth direction
Does stack grow upward or downward?

Hi all,

How would I find out if a machine’s stack grows up or down in memory in C. More importantly, which is better: a system in which the stack grows up or one in which a stack grows down ?

And will below logic work ???

void sub(int *a) 
{
int b;

if (&b > a) {
printf("Stack grows up.");
}
else  
{
printf("Stack grows down.");
}
}
main ()
{
int a;
sub(&a);
}

I mean this expression is valid in C

if (&b > a) 

回答1:


Stacks don't even have to exist in the first place, and when they do, they don't have to work in the way you expect. They can be created dynamically, for example, or they could just jump around in memory. Your question isn't really a pure "C" question, because it isn't portable.

However, assuming that stacks either grow up or grow down, then you can just check this by taking the address of a variable inside a caller function and also inside a callee, and checking to see which one is greater; it's straightforward.



来源:https://stackoverflow.com/questions/5361123/how-will-you-find-out-if-a-stack-grows-up-or-down-in-a-system

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!