为局部变量分配空间 [英] allocating space for local variables

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

问题描述

大家好,


考虑以下代码:


func()

{


if(x == 0){

int arr [50];

/ *做点什么* /

}

else {

int arr2 [100];

/ *做点什么* /

}

}

我想知道,如果arr [50的空间仅在

条件下分配时x = = 0"

是真的,或者在函数func时分配空格。被叫。

我正在开发一个m68k实现,其中本地变量是

分配

在堆栈上。如果有人能为m68k

实施提供一个

答案,我将非常感激。


非常感谢任何人提前帮忙......

Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}
I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

thanks a lot for any help in advance ...

推荐答案

ju ********** @ yahoo.co.in 写道:
大家好,

考虑以下代码:

func()
{

if(x == 0){
int arr [50];
/ *做点什么* /
}
其他{
int arr2 [100];
/ *做点什么* /
}
}

我想知道,如果arr [50的空间仅在
条件x == 0时分配。当函数func时,
为真,或者分配空间。被称为。
我正在开发一个m68k实现,其中本地变量被分配在堆栈上。如果有人能为我提供m68k的答案,我将非常感激
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}
I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k




答案将取决于编译器,编译器标志依赖,以及

代码依赖。一些代码,取决于优化/等。可能是

重新排序,玩的东西与一些非常不同的代码非常不同。


你最好的选择是看看程序集(如果可用 - gcc -S for gcc)

为你编译的确切代码生成了你将使用的选项

用于生产"。



The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.

Your best bet is to peek at the assembly(if available - gcc -S for gcc)
generated for the exact code you have compiled with the options you''ll
be using in "production".


ju ***** *****@yahoo.co.in 发布:
ju**********@yahoo.co.in posted:
大家好,

请考虑以下代码:

func()
{
如果(x == 0){
int arr [50];
/ *做点什么* /
}
其他{
int arr2 [100];
/ *做点什么* /
}
}

我想要的要知道,如果arr [50的空间仅在
条件x == 0时分配。当函数func时,
为真,或者分配空间。被称为。
我正在开发一个m68k实现,其中本地变量被分配在堆栈上。如果有人能为我提供m68k
实施的答案,我将非常感激。

非常感谢您提前帮助...
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}
I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
implementation.

thanks a lot for any help in advance ...




您可以使用匿名联盟。 (请纠正我,如果他们不是

标准C)。


union {

int arr [50];

int arr2 [100];

};

-


Frederick Gotham



You could use an anonymous union. (Please correct me if they''re not
Standard C).

union {
int arr[50];
int arr2[100];
};
--

Frederick Gotham




Nils O. Sel?sdal写道:

Nils O. Sel?sdal wrote:
ju ********** @ yahoo.co.in 写道:
大家好,
考虑以下代码:

func()
{
如果(x == 0){
int arr [50];
/ *做点什么* /
}
其他{
int arr2 [100];
/ *做点什么* /
}
}

我想知道,如果arr [50的空间仅在
条件x == 0时分配。当函数func时,
为真,或者分配空间。被称为。
我正在开发一个m68k实现,其中本地变量被分配在堆栈上。如果有人能为m68k提供答案,我将非常感激
答案将取决于编译器,依赖于编译器标志,并且依赖于代码。一些代码,取决于优化/等。可能会重新排序,以完全不同于一些非常不同的代码。
Hi guys,

Consider the following piece of code:

func()
{

if(x==0) {
int arr[50];
/* do something */
}
else {
int arr2[100];
/* do something */
}
}
I wanted to know, if the space of arr[50 is allocated only when the
condition "x==0" is
true or the space is allocated when the function "func" is called.
I am working on an m68k implementation where the local varialbles are
allocated
on stack. I would be really grateful if anyone can provide me an
answer for an m68k
The answer would be compiler dependant, compiler flags dependant, and
code dependant. Some code , depending on the optimizations/etc. might be
reordered to play things out very differently than just some very
slightly different code.




如果我错了请纠正我。

内存将分配给两个阵列,因为它发生在

编译时间。

这里malloc出现了。它会在运行时完成。


再一次,我告诉它只是我的理解。如果错误纠正了我。

最好的办法是窥视装配(如果可用的话 - gcc -S for gcc)
根据您编译的确切代码生成
ll
在生产中使用。



Please correct me if i''m wrong.
Memory will be allocated for both arrays since it happens at
compilation time.
Here malloc comes into picture. It ''ll do it at run time.

Once again i''m telling it''s only my understanding. If wrong correct me.
Your best bet is to peek at the assembly(if available - gcc -S for gcc)
generated for the exact code you have compiled with the options you''ll
be using in "production".






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

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