将堆栈用于局部变量的背后的想法是什么? [英] What is the idea behind using a stack for local variables?

查看:87
本文介绍了将堆栈用于局部变量的背后的想法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在C语言中,堆栈是所有局部变量所在的位置.栈是先进先出的数据结构,这意味着您只能访问最近推送到栈中的内容.因此,给出以下代码:

In C as many of you know, the stack is where all local variables reside. The stack being a first in last out data structure means you can only access what has been most recently pushed onto it. So given the following code:

int k = 5;
int j = 3;
short int i;

if (k > j) i = 1;

很明显,这是无用的代码,没有真正的意义,但是我试图将自己的头缠在某个东西上.

Obviously this is useless code which has no real meaning but I'm trying to wrap my head around something.

对于简短的int i声明,我假设在堆栈上分配了2个字节.对于int k和int j,这4个字节都分配了值5和3.因此堆栈如下所示

For the short int i declaration I'm assuming 2 bytes get allocated on the stack. For int k and int j for both 4 bytes get allocated with the values 5 and 3. So the stack would look as follows

----------   <- stack pointer
int i
----------
int k = 5
----------
int j = 3
----------

因此,对于if语句,您将必须弹出int才能达到条件k和j,如果是,则int到哪里去了?如果这是C执行局部变量的方式,那么这一切似乎都非常耗时且乏味.

so for the if statement you would have to pop the int i to get to the conditions k and j, and if so where does int i go? This all seems very time consuming and tedious if this is the way that C does local variables.

那么这实际上是C的工作方式还是我将其全部丢掉了?

So is this actually how C does it or am I mucking it all up?

推荐答案

您是如此的轻巧.

是的,局部(auto)变量通常存储在堆栈中.但是,它们在读取时不会弹出堆栈.它们由与堆栈指针的偏移量引用.

Yes, local (auto) variables are typically stored on a stack. However, they're not popped off the stack when read; they're referenced by an offset from the stack pointer.

采用以下代码:

x = y + z;

其中,每个xyz都分配在堆栈上.当编译器生成等效的机器代码时,它将通过给定寄存器的偏移量引用每个变量,类似于:

where each of x, y, and z are allocated on the stack. When the compiler generates the equivalent machine code, it will refer to each variable by an offset from a given register, sort of like:

mov -8(%ebp), %eax   
add -12(%ebp), %eax
mov %eax, -4(%ebp)

在x86架构上,%ebp帧指针;堆栈被分解为 frames ,其中每个框架包含函数参数(如果有),返回地址(即函数调用后的指令的地址)和局部变量(如果有的话) ).在我熟悉的系统上,堆栈朝"0"方向向下"增长,局部变量存储在帧指针"下方(较低的地址),因此为负偏移量.上面的代码假定x-4(%ebp),y在-8(%ebp)z-12(%ebp).

On x86 architectures, %ebp is the frame pointer; the stack is broken up into frames, where each frame contains function parameters (if any), the return address (that is, the address of the instruction following the function call), and local variables (if any). On the systems I'm familiar with, the stack grows "downwards" towards 0, and local variables are stored "below" the frame pointer (lower addresses), hence the negative offset. The code above assumes that x is at -4(%ebp), y is at -8(%ebp), and z is at -12(%ebp).

该函数返回时,所有内容将从堆栈 1 中弹出,但不会在此之前弹出.

Everything will be popped off the stack1 when the function returns, but not before.

编辑

请注意,C语言定义要求 none .该语言完全不需要使用运行时堆栈 (尽管如果没有一个编译器,实现起来很麻烦).它仅将auto变量的生存期定义为从声明结束到其封闭范围的结束.堆栈使操作变得简单,但这不是必需的.

Please note that none of this is mandated by the C language definition. The language does not require the use of a runtime stack at all (although a compiler would be a bitch to implement without one). It merely defines the lifetime of auto variables as being from the end of their declaration to the end of their enclosing scope. A stack makes that easy, but it's not required.


1.好了,堆栈指针和帧指针将被设置为新值.数据将保留在原来的位置,但是现在该内存可供其他使用.

这篇关于将堆栈用于局部变量的背后的想法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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