堆栈增长方向 [英] direction of stack growth

查看:64
本文介绍了堆栈增长方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我们有什么方法可以确定筹码的方向

增长(来自

较高地址到较低地址或从较低地址到较高地址

地址)?

我知道这个问题是特定于实现的,这可能不是

正确的地方

问这个。但任何提示都会对我有所帮助。


我想到了以下方式(假设实现使用

堆栈来

保存局部变量,例如在m68k处理器中)。

在函数f1()中,声明一个局部变量(local1)。

保存其地址。从f1调用另一个函数f2(),在f2()中调用
声明另一个本地

变量(local2)并将其地址返回给f1()。现在,比较local1

和local2的

地址,以确定堆栈增长的方向。


这是正确的方法这样做?我们可以比较

本地变量的地址

local1和local?如果没有,这样做的权利是什么?

此外,一些实现不使用堆栈来存储本地

变量。他们将寄存器中的本地变量存储为
。在那些实现中,如果我尝试使用
打印局部变量的地址(使用& local1),会发生什么?b $ b?我问这个,

因为当地的

变量可能没有在堆栈或某些内存位置上分配。

编译器

可能已经使用了一些通用寄存器。在这种情况下,

如果我们打印一个局部变量的地址,我们将获得什么输出?b / b
Thanx任何帮助/提示。 ...

解决方案

ju ********** @ yahoo.co.in 写道:

大家好,

有没有什么方法可以确定堆栈的增长方向(从更高的地址到更低的地址或从更低的地址到更高的地址)?




没有可移植的方法来做到这一点。在给定的实现中,

问题甚至可能毫无意义。 (考虑在

a Lispy机器上的C实现,其中堆栈帧是从堆中分配的。)


无论如何,你为什么要知道?


-

Chris" not a Tuvela" Dollin

要说人类是这样的,所以几乎总是自动撒谎。




Chris Dollin写道:

ju ********* *@yahoo.co.in 写道:

大家好,

有什么方法可以确定堆栈的方向<增长(从较高地址到较低地址或从较低地址到较高地址)?



没有可移植的方法来执行此操作。在给定的实现中,
问题甚至可能毫无意义。 (考虑一个Lispy机器上的C实现,其中堆栈帧是从堆中分配的。)

为什么你想知道呢?




我知道,这不可能是便携式的。但是,你能否建议一些特定的实施方式(例如摩托罗拉的68k处理器)?


"菊********** @ yahoo.co.in" <菊********** @ yahoo.co.in>写道:

大家好,

我们有什么方法可以确定堆栈增长的方向(从较高地址到较低地址或从较低地址
地址到更高的地址)?我知道这个问题是具体的实施,这可能不是问这个的正确的地方。但任何
提示都会对我有所帮助。


没有可移植的方法来确定这个。

我想到了以下方式(假设实现
使用堆栈来保存本地变量例如在m68k
处理器中)。在函数f1()中,声明一个局部变量(local1)。
保存其地址。从f1调用另一个函数f2()并在f2()中声明另一个局部变量(local2)并将其地址返回到
f1()。现在,比较local1和local2的地址,以确定堆栈增长的方向。


指针值的关系运算符只有在两个指针都指向同一个对象(或者刚好超过它的末尾)时才会被定义。

比较像& local1< &安培; local2"调用未定义的行为。


在大多数实现中,它可能会像你期望的那样工作,但是标准不保证它是b $ b。 br />

这是正确的做法吗?我们可以比较局部变量local1和local的地址吗?如果没有,
这样做的权利是什么?此外,一些实现不使用堆栈来存储局部变量。他们将本地变量存储在
寄存器中。在那些实现中,如果我尝试打印局部变量的地址(使用& local1)会发生什么?我问这个,
因为局部变量可能没有在堆栈或某些内存位置上分配。编译器可能已经使用了一些通用的
寄存器。在这种情况下,如果我们打印一个局部变量的地址,我们将获得什么输出?




您可以合法地获取任何对象的地址'没有用注册声明

关键词。如果实现选择将对象存储在寄存器中,它还必须将它存储在内存中,如果你把它的地址拿到它的b $ b。 (最有可能的是,取一个对象的地址会导致编译器*而不是*将*存储在寄存器中。)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?
I know this question is implementation specific and this may not be the
correct place
to ask this. But any hints would help me a lot.

I thought of the following way (assuming that the implementation uses
stack to
save the local variables as for instance in m68k processor).
In a function f1(), declare a local variable (local1).
save its address. Call another function f2() from f1 and in f2()
declare another local
variable (local2) and return its address to f1(). Now, compare the
addresses of local1
and local2 to determine the direction of stack growth.

Is this the right way of doing it ? Can we compare the adresses of the
local variables
local1 and local ? If not, what is the right of doing it ?
Also, some of the implementations don''t use stack to store the local
variables. They store
the local varaibles in registers. In those implementations what would
happen if I try
to print address of local variable (using &local1) ? I am asking this,
as the local
variable may not have been allocated on stack or some memory location.
The compiler
might have used some general purpose register for that. In that case,
what output
we will get if we print the address of a local variable ?
Thanx for any help/hint ....

解决方案

ju**********@yahoo.co.in wrote:

Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?



There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?

--
Chris "not a Tuvela" Dollin
"To say that the human is thus and so is almost always to lie automatically."



Chris Dollin wrote:

ju**********@yahoo.co.in wrote:

Hi all,

Is there any way by which we mat determine the direction of stack
growth (from
higher address to lower address Or from lower address to higher
address) ?



There is no portable way to do this. On an given implementation, the
question might even be meaningless. (Consider a C implementation on
a Lispy machine where stack frames are allocated from the heap.)

Why do you want to know, anyway?



I know, this cannot be done portably. But, can you suggest some way for
a
specific implementation (as for instance motorola''s 68k processor) ?


"ju**********@yahoo.co.in" <ju**********@yahoo.co.in> writes:

Hi all,

Is there any way by which we mat determine the direction of
stack growth (from higher address to lower address Or from lower
address to higher address) ? I know this question is implementation
specific and this may not be the correct place to ask this. But any
hints would help me a lot.
There is no portable way to determine this.
I thought of the following way (assuming that the implementation
uses stack to save the local variables as for instance in m68k
processor). In a function f1(), declare a local variable (local1).
save its address. Call another function f2() from f1 and in f2()
declare another local variable (local2) and return its address to
f1(). Now, compare the addresses of local1 and local2 to determine
the direction of stack growth.
Relational operators on pointer values are defined only when both
pointers point within the same object (or just past the end of it).
A comparison like "&local1 < &local2" invokes undefined behavior.

It''s likely to work as you expect on most implementations, but it''s
not guaranteed by the standard.

Is this the right way of doing it ? Can we compare the adresses of
the local variables local1 and local ? If not, what is the right of
doing it ? Also, some of the implementations don''t use stack to
store the local variables. They store the local varaibles in
registers. In those implementations what would happen if I try to
print address of local variable (using &local1) ? I am asking this,
as the local variable may not have been allocated on stack or some
memory location. The compiler might have used some general purpose
register for that. In that case, what output we will get if we print
the address of a local variable ?



You can legally take the address of any object that''s not declared
with the "register" keyword. If the implementation chooses to store
the object in a register, it must also store it in memory if you take
its address. (Most likely, taking the address of an object will cause
the compiler *not* to store it in a register.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于堆栈增长方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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