堆栈是否向上或向下的成长? [英] Does stack grow upward or downward?

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

问题描述

我有这块code在C:

I have this piece of code in c:

int q = 10;
int s = 5;
int a[3];

printf("Address of a: %d\n",    (int)a);
printf("Address of a[1]: %d\n", (int)&a[1]);
printf("Address of a[2]: %d\n", (int)&a[2]);
printf("Address of q: %d\n",    (int)&q);
printf("Address of s: %d\n",    (int)&s);

输出是:

Address of a: 2293584
Address of a[1]: 2293588
Address of a[2]: 2293592
Address of q: 2293612
Address of s: 2293608

所以,我看到,从 A A [2] ,存储器地址由4个字节增加。
但是从取值,内存地址由4个字节下降。

So, I see that from a to a[2], memory addresses increases by 4 bytes each. But from q to s, memory addresses decrease by 4 byte.

我不知道两件事:


  1. 堆栈是否成长向上或向下? (它看起来既像我在这种情况下)

  2. 什么之间发生一个[2] 内存地址?为什么有很大的区别的内存吗? (20字节)。

  1. Does stack grow up or down? (It looks like both to me in this case)
  2. What happen between a[2] and q memory addresses? Why there are a big memory difference there? (20 bytes).

请注意:这不是功课的问题。我对堆栈是如何工作的好奇。感谢您的帮助。

Note: This is not homework question. I am curious on how stack works. Thanks for any help.

推荐答案

协议栈的行为(成长或成长向下)取决于应用程序二进制接口(ABI),以及如何调用堆栈(又名启动记录)举办。

The behavior of stack (growing up or growing down) depends on the application binary interface (ABI) and how the call stack (aka activation record) is organized.

纵观其生命周期计划势必像OS以外的程序进行通信。 ABI决定程序如何能与另一个程序进行通信。

Throughout its lifetime a program is bound to communicate with other programs like OS. ABI determines how a program can communicate with another program.

堆栈为不同的体系结构可以生长的任何一种方式,但对于一个体系结构将是一致的。请检查维基链接。但是,堆栈的增长是由该体系结构的ABI决定。

The stack for different architectures can grow the either way, but for an architecture it will be consistent. Please check this wiki link. But, the stack's growth is decided by the ABI of that architecture.

例如,如果你把MIPS ABI,调用堆栈的定义如下。

For example, if you take the MIPS ABI, the call stack is defined as below.

让我们考虑功能fn1的'通话'FN2。现在,通过FN2看到堆栈帧如下:

Let us consider that function 'fn1' calls 'fn2'. Now the stack frame as seen by 'fn2' is as follows:

direction of     |                                 |
  growth of      +---------------------------------+ 
   stack         | Parameters passed by fn1(caller)|
from higher addr.|                                 |
to lower addr.   | Direction of growth is opposite |
      |          |   to direction of stack growth  |
      |          +---------------------------------+ <-- SP on entry to fn2
      |          | Return address from fn2(callee) | 
      V          +---------------------------------+ 
                 | Callee saved registers being    | 
                 |   used in the callee function   | 
                 +---------------------------------+
                 | Local variables of fn2          |
                 |(Direction of growth of frame is |
                 | same as direction of growth of  |
                 |            stack)               |
                 +---------------------------------+ 
                 | Arguments to functions called   |
                 | by fn2                          |
                 +---------------------------------+ <- Current SP after stack 
                                                        frame is allocated

现在你可以看到栈是向下生长。所以,如果该变量被分配给函数的局部框架,该变量的地址实际向下增长。编译器可以对内存分配变量的顺序决定。 (在你的情况下,它可以是'Q'或'S'是第一次分配堆栈存储器。但是,一般的编译器堆栈内存分配根据的变量声明的顺序)。

Now you can see the stack grows downward. So, if the variables are allocated to the local frame of the function, the variable's addresses actually grows downward. The compiler can decide on the order of variables for memory allocation. (In your case it can be either 'q' or 's' that is first allocated stack memory. But, generally the compiler does stack memory allocation as per the order of the declaration of the variables).

但在阵列的情况下,分配有唯一的单个指针和存储需要分配将由单个指针被实际指出。存储器需要是连续的数组。因此,尽管堆栈增长下降,对数组堆栈长大。

But in case of the arrays, the allocation has only single pointer and the memory needs to be allocated will be actually pointed by a single pointer. The memory needs to be contiguous for an array. So, though stack grows downward, for arrays the stack grows up.

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

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