声明函数时使用的内存 [英] memory used while declaring function

查看:59
本文介绍了声明函数时使用的内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

声明函数时堆栈中是否分配了内存


例如void fun(int a,int b,int c);


是否真的编译器为参数a,b

和c ....保留3个堆栈空间。

Is there any memory allocated in stack when declaring a function

e.g void fun(int a,int b,int c);

Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....

推荐答案

11月13日,7:18 * am,c.lang.mys ... @ gmail.com

< c.lang.mys ... @ gmail.comwrote:
On Nov 13, 7:18*am, "c.lang.mys...@gmail.com"
<c.lang.mys...@gmail.comwrote:

声明函数时堆栈中是否分配了内存


例如void fun(int a,int b,int c );


编译器为参数a,b

和c保留3个堆栈空间是真的吗。
Is there any memory allocated in stack when declaring a function

e.g void fun(int a,int b,int c);

Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....



编号甚至没有要求堆栈这样的东西,即使是
存在。

-

Fred

No. There isn''t even a requirement that such a thing as a stack even
exists.
--
Fred


c。** *********@gmail.com 写道:
c.***********@gmail.com wrote:

是否分配了任何内存n声明函数时堆栈


例如void fun(int a,int b,int c);
Is there any memory allocated in stack when declaring a function

e.g void fun(int a,int b,int c);



什么堆栈?


代码没有运行,所以即使运行时有堆栈,



编译器处理声明时没有一个。


(除非你问关于/ compiler'/ stack,如果有的话;如果

那么,为什么?)

What stack?

The code isn''t running, so even if there''s a stack at runtime,
there isn''t one when the declaration gets processed by the
compiler.

(Unless you''re asking about the /compiler''s/ stack, if any; if
so, why?)


编译器保留3是真的吗?参数堆栈空间a,b

和c ....
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....



No.


当编译器处理/ declaration /时,它不需要为任何东西保留

堆栈空间。该声明只是说'有'这个

函数`fun`,带有参数`(int,int,int)`和return-type void。"


参数是否在堆栈上传递无关紧要,因为

我们也没有编译调用`fun`或它的定义。


当我们编译`void fun(int a,int b,int c){... the body ...}`,

/ then /我们可能考虑堆栈分配,如果这就是

实现的运作方式。代码的行为是什么/需要/是
好​​像有三个自动变量a,b,c,初始化为值

传递给当函数退出时,可以忘记调用。


考虑


int fun(int a,int b,int c){返回a + b + c; }


我已经返回`int`来发生一些事情。当

编译器编译此代码时,它不需要使用任何堆栈/,/

甚至没有:


添加r0,r1

添加r0,r2

mov pc,r14


参数在寄存器r0,r1中传递,和r2,并在r0中返回

结果。可以在结果/第一个参数寄存器中就地添加到

中。由于`fun`并没有调用

其他任何东西,我们不需要堆叠一个返回地址或复制

的论点。


因此,虽然一个实现/可能/使用堆栈来实现功能

业务,但特定情况可以无堆栈。


stoty的道德之处在于,不应该将

实现技术(堆栈)与规范(变量

,当它们的范围留下时消失)混淆,也不应该声明(让我告诉你关于X)和定义(编译我这个X)和

执行(调用X并传递这三个值)。


-

这只是我们见过的开始。 - 斗兽场,/明天的蓝调/

Hewlett-Packard Limited注册办事处:Cain Road,Bracknell,

注册号:690597 England Berks RG12 1HN

No.

When the compiler processes that /declaration/, it needn''t reserve
stack space for anything. The declaration just says "there''s this
function `fun` with arguments `(int, int, int)` and return-type void."

Whether arguments are passed on a stack or not doesn''t matter, since
we''re not compiling a call of `fun` or a definition of it either.

When we compile `void fun(int a, int b, int c) { ...the body ... }`,
/then/ we might think about stack allocation, if that''s how the
implementation operates. What''s /required/ is that the code behaves
as if there''s three auto variables a, b, c, initialised to the values
passed in a call, and which can be forgotten when the function exits.

Consider

int fun( int a, int b, int c ) { return a + b + c; }

where I''ve returned `int` to have something happening. When the
compiler compiles this code it need not use any stack /at all/,
not even a bit:

add r0, r1
add r0, r2
mov pc, r14

The arguments are passed in registers r0, r1, and r2, and the
result returned in r0. The addition can be done in-place into
the result/first-argument register. Since `fun` doesn''t call
anything else, we don''t need to stack a return address or copies
of the arguments.

So, while an implementation /might/ use a stack for function
business, specific cases can be stack-free.

The moral of the stoty is that one shouldn''t confuse an
implementation technique (stacks) with a specification (variables
which vanish when their scope is left), nor declarations (let
me tell you about X) with definitions (compile me this X) and
executions (call X and pass these three values).

--
"It''s just the beginning we''ve seen." - Colosseum, /Tomorrow''s Blues/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN


c。*** ********@gmail.com 写道:

声明函数时堆栈中是否分配了内存


例如void fun(int a,int b,int c);
Is there any memory allocated in stack when declaring a function

e.g void fun(int a,int b,int c);



C语言没有描述实现的实现方式

它需要什么,所以它不可能回答你的问题

完全一般性。对于它的价值,我从未遇到过

一个实现,它使用任何堆栈空间来完成一个函数

声明。

The C language does not describe how an implementation does
what is required of it, so it''s impossible to answer your question
in full generality. For what it''s worth, I have never come across
an implementation that used any stack space at all for a function
declaration.


编译器是否为参数a,b

和c ....预留3个堆栈空间是真的。
Is it true that compiler reserves 3 stack spaces for parameters a ,b
and c....



如上所述:可能,但几乎肯定不是。语言

标准并不禁止为声明保留堆栈空间,

但我从未见过这样做的实现。


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


这篇关于声明函数时使用的内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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