静态分配问题...... [英] static allocation question...

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

问题描述

- 我在c.l.c中没有注意到这个具体的

。常见问题解答,我也不能从我的C标准副本中得到一份海峡问题

。希望有人

- 这里可以解释一下:)...


6.2.1

如果声明

的声明符或类型说明符标识符出现在块内或函数定义中

参数声明列表中,

标识符有块范围,它终止于相关块的

结尾。


我相信这会谈到我们早期学到的东西:


int main(无效)

{

{

int a;

}

printf("%d \ n",a); / *错误 - 无效* /

返回0;

}


一旦在结束之外} a不再可访问。

它超出了我们当前的范围。这让我想到了我的

问题。由于该数据是静态的,是否会在程序启动时自动分配



int main(无效)

{

int i;

返回0;

}


As" i"在这个例子中?如果那里有条件的

声明怎么办?如:


int main(无效)

{

int i = 0;

if(!i){

int a;

}

返回0;

}


如果是a is / NOT /自动分配了i,编译器做了什么,以确保它只在以下情况下分配,

以及是否需要它(如果i在

之前更改if,或者如果它等待来自用户的输入,我们将假设

是未知的,直到运行时将a仍然分配?)


任何帮助都非常感谢,只是我开始的事情

想知道:) FMorales ......

-- I didn''t notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It''s out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there''s a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it''s ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we''ll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...

推荐答案

你试过这个吗?

在C中你必须先声明所有变量然后再启动你的代码。

所以你必须这样做

int a;

int i;


你做了什么是允许的在c ++中。我的意思是这段代码:
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
int main(void)
{i / 0;
if if(!i){
int a;
}
返回0;
}


不能编译。


FMorales写道: - - 我在clc中没有注意到这个具体的内容常见问题解答,我也不能从我的C标准副本中得到一个严厉的回答。希望有人
- 这里可以说明一下:)...

6.2.1
如果宣告者或类型说明者声明了
标识符出现在块内或函数定义中的参数声明列表中,
标识符具有块作用域,它终止于相关块的
末尾。

我相信这会谈到我们早期学到的东西:

int main(无效)
{
{
int a;
}
printf("%d \ n",a); / *错误 - 无效* /
返回0;
}

一旦在结束之外} a不再可访问。
它不在我们的当前的范围。这让我想到了我的问题。由于该数据是静态的,是否在程序启动时自动分配?

int main(void)
{
int i;
返回0 ;
}

作为我在这个例子中?如果那里有条件
声明怎么办?如:

int main(无效)
{i / 0}
如果(!i){
int a;
}
返回0;
}

如果a is / NOT /自动分配了i,编译器正在做什么,以确保它只在以下情况下分配,
以及是否需要(如果i更改)在
if之前,或者如果它等待来自用户的输入,我们将假设
是未知的,直到运行时a仍然会被分配?)

任何帮助都非常感谢,只是我开始的事情
想知道:) FMorales ......
int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}
Won''t compile.

FMorales wrote: -- I didn''t notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It''s out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there''s a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it''s ever needed (like if "i" changes before the
"if", or if it waits for input from a user which we''ll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...






周六,2003年10月4日18:09:53 -0500,Pushkar Pradhan写道:
On Sat, 04 Oct 2003 18:09:53 -0500, Pushkar Pradhan wrote:
你试过吗?
在C中你必须先声明所有变量,然后开始你的代码。


只在c89

在c99,变量可以在任何地方声明。

所以你必须这样做
int a;
int i;

你在c ++中允许做什么。我的意思是这段代码:
Did you try this?
In C you have to declare all variables first and then start your code.
only in c89
in c99, variables can be declared anywhere.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i){
> int a;
> }
>返回0;
> }
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }



不会编译。

FMorales写道:



Won''t compile.

FMorales wrote:

- 我没有注意到这一点具体
- 在clc中常见问题解答,我也不能从我的C标准副本中得到一个严厉的回答。希望有人
- 这里可以说明一下:)...

6.2.1
如果宣告者或类型说明者声明了
标识符出现在块内或函数定义中的参数声明列表中,
标识符具有块作用域,它终止于相关块的
末尾。

我相信这会谈到我们早期学到的东西:

int main(无效)
{
{
int a;
}
printf("%d \ n",a); / *错误 - 无效* /
返回0;
}

一旦在结束之外} a不再可访问。
它不在我们的当前的范围。这让我想到了我的问题。由于该数据是静态的,是否在程序启动时自动分配?

int main(void)
{
int i;
返回0 ;
}

作为我在这个例子中?如果那里有条件
声明怎么办?如:

int main(无效)
{i / 0}
如果(!i){
int a;
}
返回0;
}


任何好的编译器都不会为你的条件语句生成代码

除非你设置我要挥发。

如果a is / NOT /自动分配了i,编译器正在做什么,以确保它只在以下情况下分配,
以及是否需要(如果i更改)在


之前,编译器不能假设除非你提供关键字static

" if",或者它等待用户的输入我们假设
是未知的,直到运行时a仍然会被分配?)

任何帮助都非常感谢,只是我开始的东西
想知道:) FMorales ......
-- I didn''t notice anything about this specifically
-- in the c.l.c. FAQ, nor could i get a strait answere
-- from my copy of the C standard. Hopeing someone
-- here could shed some light on it :)...

6.2.1
"If the declarator or type specifier that declares
the identifier appears inside a block or within the
list of parameter declarations in a function definition,
the identifier has block scope, which terminates at the
end of the associated block."

I believe this talks about what we learned early on :

int main(void)
{
{
int a;
}
printf("%d\n", a); /* ERROR - Invalid */
return 0;
}

Once outside of the closing } a is no longer accessible.
It''s out of our current "scope". Which brings me to my
question. Since that data is static, is it automatically
allocated at program start up?

int main(void)
{
int i;
return 0;
}

As "i" would be in this example ? What if there''s a conditional
statement there ? As in :

int main(void)
{
int i = 0;
if(!i) {
int a;
}
return 0;
}
any good compiler won''t generate code for your conditional statement
unless you set i to be "volatile".

If "a" is /NOT/ automatically allocated with "i", what is
the compiler doing to make sure it gets allocated only when,
and if it''s ever needed (like if "i" changes before the
the compiler can''t assume that unless you provide the keyword static
"if", or if it waits for input from a user which we''ll assume
is unknown untill runtime will "a" still get allocated ?)

Any help is greatly appreciated, just something i started
wondering about :) FMorales...






Pushkar Pradhan写道:
Pushkar Pradhan wrote:
你试过吗?这个?
在C中你必须首先声明所有变量,然后开始你的代码。
所以你必须做
int a;
int i;
<你在c ++中允许做什么。我的意思是这段代码:
Did you try this?
In C you have to declare all variables first and then start your code.
So you would have to do
int a;
int i;

What you did is allowed in c++. I mean this code:
> int main(void)
> {
> int i = 0;
> if(!i){
> int a;
> }
>返回0;
> }
> int main(void)
> {
> int i = 0;
> if(!i) {
> int a;
> }
> return 0;
> }



普什卡,你必须暂时遭遇疯狂。

在块的开头声明变量没有错。上面的

代码是完全有效的C89(当然还有C99)。


-

Bertrand Mollinier Toublet

关闭= 1.0 / farthitude

- Arthur J. O''Dwyer


Pushkar, you must have suffered a temporary access of madness. There is
nothing wrong with declaring variables at the beginning of a block. The
code above is perfectly valid C89 (as well as C99, of course).

--
Bertrand Mollinier Toublet
closity = 1.0 / farthitude
-- Arthur J. O''Dwyer


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

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