编译器如何在堆栈上排列局部变量? [英] How does compiler arrange local variables on stack?
问题描述
我们知道,局部变量位于栈上.然而,他们的顺序是什么?是按照申报的先后顺序排列的吗?这意味着第一个声明的变量被安排在堆栈的高地址(堆栈增长到低地址)?举个例子:
As we know, local variables is located on stack. However, what is their order? Are they arranged as the order of their declaration? This means that the first declared variable is arrange on the higher address of stack (stack grows to lower address) ? As an example:
void foo(){
int iArray[4];
int iVar;
}
在栈上,局部变量——iArray
和iVar
是这样排列的?
On stack, the local variable-- iArray
and iVar
are arranged as followed?
推荐答案
最简单的实现使预测各种变量在堆栈中的最终位置变得非常容易.但是,这些实现也存在某些安全问题(主要是缓冲区溢出和预测额外数据将覆盖的内容,允许注入 shellcode).
The simplest implementations make it very easy to predict where various variables will end up on the stack. However, those implementations also allow certain security problems (mainly, overflowing a buffer and predicting what the extra data will overwrite, allowing the injection of shellcode).
由于堆栈的布局是在大多数基于堆栈的语言中定义的实现(从技术上讲,许多此类语言不强制使用堆栈,而是具有易于实现的语义使用堆栈),编译器编写者已经竭尽全力使其难以预测运行时的堆栈布局一>.
Since the layout of the stack is implementation defined in most stack-based languages (technically, many such languages don't mandate the use of a stack, but instead have semantics that are easy to implement with a stack), compiler writers have gone to great lengths to make it hard to predict the stack layout at runtime.
这篇关于编译器如何在堆栈上排列局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!