for循环中的变量声明 [英] declaration of variable in for loop

查看:112
本文介绍了for循环中的变量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


假设我有一些代码如

main()

{

int i,j;

for(i = 0; i< 10; i ++){

int k = i;

printf("%d%p = *%d \ n",i,& k,k);

}

}

当我在所有迭代中看到K的地址相同时。这意味着K只是一次定义一次的


是因为每个for循环执行是被认为是separte

块,它在同一地址分配?

或者只分​​配一次内存?

Hi there,

suppose i have piece of code like
main()
{
int i,j;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);
}
}
When i see the address of K its same in all iterations.That means K is
only defined once at a time.
Is it because each for loop execution is considered as separte
block,its allocating in the same address?
or is it allocated memory only once?

推荐答案

文章< 05 ********************************** @ f10g2000hsf。 googlegroups.com>,

< po ************* @ gmail.comwrote:
In article <05**********************************@f10g2000hsf. googlegroups.com>,
<po*************@gmail.comwrote:

>假设我有一段代码如
main()

int i,j;

for(i = 0; i< 10; i ++){

int k = i;

printf("%d%p = *%d \ n",i,& k,k );
}
}
>suppose i have piece of code like
main()
{
int i,j;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);
}
}


>当我在所有迭代中看到K的地址相同时。这意味着K只能一次定义一次。
是因为每个for循环执行被认为是separte
块,它在同一地址分配?
或者是它只分配了一次内存?
>When i see the address of K its same in all iterations.That means K is
only defined once at a time.
Is it because each for loop execution is considered as separte
block,its allocating in the same address?
or is it allocated memory only once?



答案取决于编译器。 C没有定义何时或何地为自动变量进行内存分配:它只有

定义了何时可以有效访问存储的规则,以及

关于名称的声明是由

a提到的名称的规则。


在某些编译器中,答案是不是以上。

例如,一些编译器从

a堆中分配自动变量,因此该变量出现在特定的

地址中每次迭代都可以反映这样一个事实,即在循环的迭代之间没有发布未分配的堆分配。


-

有什么事情可以说,看,这是新的吗?它已经过去了,这已经是我们面前的旧时代了。 - Ecclesiastes

The answer is compiler dependant. C does not define when or where
memory allocation is made for automatic variables: it only
defines rules for when storage is meaningfully accessible, and
rules about which declaration of a name is the one denoted by
a mention of the name.

In some compilers the answer would be "neither of the above".
For example some compilers allocate automatic variables from
a heap, so the fact that the variable showed up with a particular
address in each iteration could just reflect the fact that no
unreleased heap allocations were made between iterations of the loop.

--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes


1月31日下午2:25,poornimampra ... @ gmail.com写道:
On Jan 31, 2:25 pm, poornimampra...@gmail.com wrote:

你好,


假设我有一些代码如

main()

{

int i,j;

for(i = 0; i< 10; i ++){

int k = i;

printf("%d%p = *%d \ n",i,& k,k);

}


}


当我看到K的地址在所有迭代中都是相同的。这意味着K只是

一次只定义一次。

是因为每个for循环执行被认为是separte

块,它在同一地址分配?

还是只分配了一次内存?
Hi there,

suppose i have piece of code like
main()
{
int i,j;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);
}

}

When i see the address of K its same in all iterations.That means K is
only defined once at a time.
Is it because each for loop execution is considered as separte
block,its allocating in the same address?
or is it allocated memory only once?



因为每个for循环执行都被视为separte block。并且

K的内存地址应该是随机的。

because each for loop execution is considered as separte block. And
the memory address of K should be random.


po ************* @ gmail.com 写道:
po*************@gmail.com wrote:

你好,


假设我有一段代码,比如
Hi there,

suppose i have piece of code like



在这里包括stdio.h。

Include stdio.h here.


main()
main()



int main(void)是更好的形式。

int main(void) is better form.


{

int i,j;

for(i = 0; i< 10; i ++){

int k = i;

printf("%d%p = *%d \ n,i,& k,k);
{
int i,j;
for(i=0;i<10;i++){
int k = i;
printf("%d %p = *%d\n",i,&k,k);



您可能想要:


printf("%d \ t%p =%d \ n" ,i,(void *)& k,k);


''p''格式说明符需要void *值和
$ b的类型由地址运算符产生的$ b值是指向T的指针。其中T是

它的操作数的类型。格式字符串中的''*''字符

指定以下格式化操作是由''*'的相应参数指定的最小

字段宽度。在这个

的情况下,它是第四个printf()参数,因此,最终的''d''

说明符没有参数,调用未定义的行为。 />

You probably want:

printf("%d\t%p = %d\n", i, (void *)&k, k);

The ''p'' format specifier expects a void * value and the type of the
value yielded by the address-of operator is "pointer to T" where T is
the type of it''s operand. Also the ''*'' character in your format string
specifies that the following formatting operation be of the minimum
field width specified by the corresponding argument to ''*''. In this
case it is the fourth printf() argument and thus, the final ''d''
specifier is left without an argument, invoking undefined behaviour.


}

}


当我看到K的地址在所有迭代中都是相同的。那么意味着K是

一次只定义一次。

是因为每个for循环执行都被认为是separte

block,它的分配是相同的地址?

或仅分配一次内存?
}
}
When i see the address of K its same in all iterations.That means K is
only defined once at a time.
Is it because each for loop execution is considered as separte
block,its allocating in the same address?
or is it allocated memory only once?



这是依赖于实现的。您不能依赖于''k'的地址。

在迭代中是相同的。然而,编译器很可能是优化的。在这种情况下,只需要在循环的整个持续时间内创建并销毁''k''一次。但你不能依赖这种行为。

This is implementation dependant. You cannot rely on the address of ''k''
being the same across iterations. However the compiler is very likely
to "optimise" in such cases and create and destroy ''k'' only once for
the entire duration of the loop. But you cannot rely on such behaviour.


这篇关于for循环中的变量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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