函数的内存分配 [英] Memory alloc for a Function

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

问题描述



这里是函数:


int sum(int a,int b)

{

int c;

c = a + b;

返回c;

}


调用函数时系统会分配多少空格

?(4字节

for int)


我仍​​然有很多问题使

我迷惑。比如

sizeof()func的返回值,在哪里为

函数的变量等。


哪本书会详细讨论这些问题




谢谢,

最好的问候!


-

Ryan Wang

电子邮件:wangrm1979#at #gmail#dot#com

msn:wangrm1979#at#hotmail#dot#com

解决方案

在文章< dl ********* @ news.cn99.com> ;,

Ryan Wang< wa ********* @ windics1 .COM>写道:

这里是函数:
int sum(int a,int b)
{
int c;
c = a + b;
返回c;
}
调用函数时系统会分配多少空格?(4字节
用于int)




内存量完全取决于系统,编译器,


我知道的系统,* no *内存将被分配。

两个整数参数将通过寄存器传递,

编译器将发出单个add ;关于那些

寄存器的说明将结果存入第三个寄存器,并且

第三个寄存器恰好是返回值
$ b $的寄存器b通常会被传回。


在某些系统上,甚至没有为

返回地址分配内存。

其他系统......我见过其他系统已经为这样的功能分配了最少86字节堆栈字节的
a。


有些海报使用的是嵌入式系统,其中*是*

没有堆叠。

-

编程就是这样发生的你正在忙着制定其他计划。


Ryan Wang写道:

这里的功能是:

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

系统在调用函数时会分配多少空格?(4个字节
for一个int)


代码和数据所需的内存量

因实现而异,不在

此处讨论的标准C.您可以查看

特定编译器的输出以确定其分配。

我仍​​然有很多问题让我感到困惑。比如返回值
sizeof()func,


sizeof以字节为单位返回对象或对象类型的大小。

在哪里分配内存对于funcs变量等等。


对于固定大小的变量,它们通常在

的开头定义它们的函数或块的开头使用(C99允许将
定义放在一个块内)。确定大小为

的变量是运行时分配的malloc()和朋友。

哪本书会详细讨论这些问题?



K& R2(Kernigan& Richie:C编程语言)是一个开始的好地方。 C FAQ< http://www.eskimo.com/~scs/C-faq/top.html>是另一个好的资源。


-

Thad


Ryan Wang < WA ********* @ windics1.com>写道:

这里是函数:
int sum(int a,int b)
{
int c;
c = a + b;
返回c;




调用函数时系统会分配多少空格?(4字节
表示int)


分配的空间因实现而异。

只要函数< b就很少有理由关注br />
正常工作。你为什么想知道?

我还有很多问题让我感到困惑。比如
sizeof()func的返回值,在哪里分配内存为函数的变量等。




sizeof是一个运算符,而不是一个函数。它以字节为单位产生其

参数的大小;参数可以是表达式或类型

括号中的名称。


-

Keith Thompson(The_Other_Keith)< a href =mailto:ks *** @ mib.org> ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Hi,
here is the function:

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

How many spaces will system alloc for
it when calling the function?(4 Bytes
for an int)

I still have a lot of questions that makes
me confuse.such as the return value of
sizeof() func, where to alloc the memory for
the variable of the funcs etc.

And which book will discuss such questions
in detail?

Thanks,
Best Regard!

--
Ryan Wang
email:wangrm1979#at#gmail#dot#com
msn:wangrm1979#at#hotmail#dot#com

解决方案

In article <dl*********@news.cn99.com>,
Ryan Wang <wa*********@windics1.com> wrote:

here is the function: int sum(int a,int b)
{
int c;
c = a + b;
return c;
} How many spaces will system alloc for
it when calling the function?(4 Bytes
for an int)



The amount of memory would depend entirely on the system, compiler,
and compiler options.

On some of the systems I know of, *no* memory would be allocated.
The two integer parameters would be passed in via registers,
the compiler would emit a single "add" instruction on those
registers depositing the result in a third register, and that
third register would happen to be the one in which the return value
would normally be passed back.

On some systems, there would not even be memory allocated for the
return adress.

Other systems... I have seen other systems that would have allocated
a minimum of 86 bytes stack bytes for a function such as that.

Some of the posters are using embedded systems on which there *is*
no stack.
--
Programming is what happens while you''re busy making other plans.


Ryan Wang wrote:

here is the function:

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

How many spaces will system alloc for
it when calling the function?(4 Bytes
for an int)
The amount of memory required for the code and for the data
varies from one implementation to another and is outside the scope of
Standard C as discussed here. You could look at the output of a
specific compiler to determine its allocation.
I still have a lot of questions that makes
me confuse.such as the return value of
sizeof() func,
sizeof returns the size of an object or object type in bytes.
where to alloc the memory for the variable of the funcs etc.
For fixed size variables, they are usually defined at the beginning of
the function or beginning of a block where they are used (C99 allows
definition to be placed within a block) . Variables that have the size
decided are runtime as allocated with malloc() and friends.
And which book will discuss such questions
in detail?



K&R2 (Kernigan & Richie: The C Programming Language) is a good place to
start. The C FAQ <http://www.eskimo.com/~scs/C-faq/top.html> is another
good resource.

--
Thad


"Ryan Wang" <wa*********@windics1.com> writes:

here is the function:

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

How many spaces will system alloc for
it when calling the function?(4 Bytes
for an int)
The allocated space will vary from one implementation to another.
There''s seldom any reason you should care, as long as the function
works properly. Why do you want to know?
I still have a lot of questions that makes
me confuse.such as the return value of
sizeof() func, where to alloc the memory for
the variable of the funcs etc.



sizeof is an operator, not a function. It yields the size of its
argument in bytes; the argument can be either an expression or a type
name in parentheses.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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