在堆栈上分配局部变量 [英] allocation of local variables on stack

查看:106
本文介绍了在堆栈上分配局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




当您在函数调用(C99)中定义varible时,

如:


if(i == 5){

int x = 5;

int z = 2;

}


它们是否在运行时遇到堆栈,或者它们之前分配了
,以及参数和初始值

变量声明?在开始执行功能代码(即语句)之前,函数激活记录应该是固定的吗?


如果这是在运行时完成的, (标准)编译器是如何做到的?


我也听说过在堆栈上分配的alloca()调用。

这是非标准功能?这样做是否有可能堆叠

溢出?


谢谢,

Bahadir

Hi,

When you define varibles in the middle of your function call (C99),
such as:

if(i == 5) {
int x = 5;
int z = 2;
}

Are they allocated on the stack as they''re encountered at run-time or
are they allocated before, along with the arguments and initial
variable declarations? Is a function activation record supposed to be
fixed before you start executing the function code (i.e. statements)?

If this is done at run-time, how does the (standard) compiler do it?

I have also heard about an alloca() call for allocating on the stack.
Is this a non-standard function? Is there a possibility of stack
overflow by doing this?

Thanks,
Bahadir

推荐答案



ba ************ @ gmail.com 写道:


当您在中间定义varibles时你的函数调用(C99),如:

if(i == 5){
int x = 5;
int z = 2;
}

它们是否在运行时遇到堆栈,或者之前是否已分配,以及参数和初始的变量声明?


它们是在编译器每次(和任何地方)选择的时候分配的。不同的编译器将使用不同的策略;

a单个编译器可能会使用不同的策略,具体取决于

其余代码。

是函数激活记录应该在开始执行功能代码(即语句)之前修复?


C没有功能激活记录。当

a函数正在执行时,存储了足够的信息

某个地方允许它返回其调用者并且可能返回值为
返回一个值,但是这是所有人都可以说的。再一次,它是编译器关于如何维护这些信息的选择,

和不同的编译器将采用不同的方式。

如果这是在运行时完成的,(标准)编译器如何做到这一点?


我不知道你在问什么 - 但无论如何,那里有
不是一个标准的编译器,没有参考实施"。

语言定义描述了运行有效程序的效果,

但不是如何实现该效果。几乎所有这些问题都由实施者自行决定,因此他们可以自由地利用平台的特性。

我也听说过在堆栈上分配的alloca()调用。
这是一个非标准函数吗?这样做是否有可能导致堆栈溢出?
Hi,

When you define varibles in the middle of your function call (C99),
such as:

if(i == 5) {
int x = 5;
int z = 2;
}

Are they allocated on the stack as they''re encountered at run-time or
are they allocated before, along with the arguments and initial
variable declarations?
They are allocated whenever (and wherever) the compiler
chooses. Different compilers will use different strategies;
a single compiler may use different strategies depending on
the rest of the code.
Is a function activation record supposed to be
fixed before you start executing the function code (i.e. statements)?
C does not have a "function activation record." While
a function is executing there is enough information stored
somewhere to allow it to return to its caller and perhaps to
return a value, but that''s about all one can say. Again, it''s
the compiler''s choice about how to maintain this information,
and different compilers will do it differently.
If this is done at run-time, how does the (standard) compiler do it?
I''m not sure what you''re asking -- but in any case, there
is no one standard compiler, no "reference implementation." The
language definition describes the effect of running a valid program,
but not how that effect is achieved. Almost all such matters are
left to the discretion of the implementors, so they can have the
freedom to exploit the special characteristics of the platform.
I have also heard about an alloca() call for allocating on the stack.
Is this a non-standard function? Is there a possibility of stack
overflow by doing this?




请参阅comp.lang.c中的问题7.32常见问题

问题(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html


-
呃********* @ sun.com



See Question 7.32 in the comp.lang.c Frequently Asked
Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

--
Er*********@sun.com


在文章< 11 ********************** @ g44g2000cwa.googlegroups .com> ;,

< ba * ***********@gmail.com>写道:
In article <11**********************@g44g2000cwa.googlegroups .com>,
<ba************@gmail.com> wrote:

当您在函数调用(C99)中定义变量时,
如:

if(i == 5 ){
int x = 5;
int z = 2;
}

是否在运行时遇到它们在堆栈上分配或
之前是否分配了它们,以及参数和初始的变量声明?

When you define varibles in the middle of your function call (C99),
such as:

if(i == 5) {
int x = 5;
int z = 2;
}

Are they allocated on the stack as they''re encountered at run-time or
are they allocated before, along with the arguments and initial
variable declarations?




编程好像你没有'不知道或不关心,并且在

a很多情况下,像这样的简单变量甚至不会是堆栈上的
。它们将被优化或放置在寄存器中。


完成后,在我对几台不同机器的观察中,堆栈通常在进入该功能时一次性分配

。但是可以有重叠:


void func()

{

int a;


{

int b;

}

{

int c;

}

}


b和c可能共享一个内存位置,那个共享的b + c内存

位置是可能是已分配在堆栈上同时

时间作为。我使用恐慌报价,因为这些是特定于实现的

概念,这些概念不属于C本身。


关于我认为这些中的任何一个问题将是

外部可见如果递归函数

在嵌套的

语句中使用了过多的堆栈空间,但是递归考虑到这个不完整的代码:


int func(int a)

{

if(a> 0)

{

/ *非平凡回报(通常)会阻止拖尾递归* /

返回other_func(a)* func(a-1);

}

else

{

struct gigantic_struct foo [1000000];

/ *执行巨大的结构数组的非平凡处理* /

/ *处理后返回非平凡值* /

}

}


除非编译器特别聪明,否则它可能会增长

即我将不得不用我最喜欢的编译器尝试这个

,看看会发生什么。

-

7842 ++



It''s better to program as if you didn''t know or care, and in
a lot of cases simple variables like that won''t even be
on the stack. They''ll be optimized away or placed in registers.

With that out of the way, in my observations of a several
different machines, the stack is usually allocated all-at-once
upon entry to the function. There can be overlap though:

void func()
{
int a;

{
int b;
}
{
int c;
}
}

b and c may share a memory location, and that shared b+c memory
location is probably "allocated" on the "stack" at the same
time as a. I use scare quotes because these are implementation-specific
concepts that aren''t part of C proper.

About the only time I think any of these issues would be
externally visible would be if a recursive function
used an excessive amount of stack space in a nested
statement, but the recursion occured outside of that.
Consider this incomplete code:

int func(int a)
{
if (a > 0)
{
/* non-trivial return (usually) prevents tail-recursion elimination */
return other_func(a) * func(a-1);
}
else
{
struct gigantic_struct foo[1000000];
/* perform non-trivial processing of gigantic struct array */
/* return non-trivial value after processing */
}
}

Unless the compiler was particularly clever, it might grow
the stack at an alarming rate. I''ll have to try this
one out with my favorite compilers and see what happens.
--
7842++


Anonymous 7843写道:
Anonymous 7843 wrote:
在文章< 11 ******************** **@g44g2000cwa.googlegroups .com> ;,
< ba ************ @ gmail.com>写道:
In article <11**********************@g44g2000cwa.googlegroups .com>,
<ba************@gmail.com> wrote:

当您在函数调用(C99)中定义变量时,
如:

if(i == 5 ){
int x = 5;
int z = 2;
}

是否在运行时遇到它们在堆栈上分配或
之前是否已经分配了参数和初始的变量声明?

When you define varibles in the middle of your function call (C99),
such as:

if(i == 5) {
int x = 5;
int z = 2;
}

Are they allocated on the stack as they''re encountered at run-time or
are they allocated before, along with the arguments and initial
variable declarations?



编程好像你不知道或者在很多情况下,像这样的简单变量甚至不会在堆栈上。它们将被优化或放置在寄存器中。

有了这个,在我对几台不同机器的观察中,堆栈通常是一次性分配的
进入该功能后。但是可以有重叠:

void func()
{
int a;

{
int b;
}
{
int c;
}
}
b和c可以共享一个内存位置,那个共享的b + c内存
位置可能是已分配的在堆栈上在同一个时间。我使用恐慌引用,因为这些是特定于实现的概念,这些概念不属于C本身。

关于我认为这些问题的唯一时间是外部的
如果递归函数在嵌套的
语句中使用了过多的堆栈空间,则可见,但是在此之外发生了递归。
考虑这个不完整的代码:

int func(int a)
{
if(a> 0)
{
/ *非平凡的返回(通常)会阻止尾递归消除* /
return other_func(a)* func(a-1);
}

{gigantic_struct foo [1000000];
/ *执行非-trivial处理巨大的结构数组* /
/ *处理后返回非平凡的值* /
}
}

除非编译器特别聪明,否则它堆栈可能会以惊人的速度增长。我将不得不尝试使用我最喜欢的编译器,看看会发生什么。
-
7842 ++



It''s better to program as if you didn''t know or care, and in
a lot of cases simple variables like that won''t even be
on the stack. They''ll be optimized away or placed in registers.

With that out of the way, in my observations of a several
different machines, the stack is usually allocated all-at-once
upon entry to the function. There can be overlap though:

void func()
{
int a;

{
int b;
}
{
int c;
}
}

b and c may share a memory location, and that shared b+c memory
location is probably "allocated" on the "stack" at the same
time as a. I use scare quotes because these are implementation-specific
concepts that aren''t part of C proper.

About the only time I think any of these issues would be
externally visible would be if a recursive function
used an excessive amount of stack space in a nested
statement, but the recursion occured outside of that.
Consider this incomplete code:

int func(int a)
{
if (a > 0)
{
/* non-trivial return (usually) prevents tail-recursion elimination */
return other_func(a) * func(a-1);
}
else
{
struct gigantic_struct foo[1000000];
/* perform non-trivial processing of gigantic struct array */
/* return non-trivial value after processing */
}
}

Unless the compiler was particularly clever, it might grow
the stack at an alarming rate. I''ll have to try this
one out with my favorite compilers and see what happens.
--
7842++




非常感谢,您的解释非常有帮助。我之所以这样问是因为我还想知道当你出现类似的情况时会发生什么样的情况。但是你的更好地强调了它。


即使你在
运行时保存了一两个数组,我个人也会关心这些事情。它没有什么大的区别,但是代码知道它并没有什么坏处.b $ b知道它。它甚至可能在有限的堆栈情况下有所帮助。


谢谢,

Bahadir



Thanks a lot, your explanation was very helpful. I asked this because I
was also wondering what would happen when a similar situation as you
exemplified would occur. But yours emphasised it better.

I personally care for these things even if you save an array or two at
runtime. It doesn''t make a big difference but it doesn''t hurt to code
knowing about it. It may even help in limited stack situations.

Thanks,
Bahadir


这篇关于在堆栈上分配局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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