开发者

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

This question already has answers here: Closed 11 years ago.

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 (&开发者_如何学Goamp;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) 


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜