您将如何确定系统中的堆栈是向上还是向下生长? [英] How will you find out if a stack grows up or down in a system?

查看:66
本文介绍了您将如何确定系统中的堆栈是向上还是向下生长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
堆栈增长方向
堆栈是向上还是向下增长?

大家好,

在 C 语言中,我如何知道机器的堆栈在内存中是向上还是向下增长.更重要的是,哪个更好:堆栈向上增长的系统还是堆栈向下增长的系统?

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) 

推荐答案

堆栈甚至不必存在,当它们存在时,它们就不必在你期望的方式.例如,它们可以动态创建,也可以直接在内存中跳转.您的问题并不是真正的纯C"问题,因为它不可移植.

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.

这篇关于您将如何确定系统中的堆栈是向上还是向下生长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆