变量发生了变化,但我没有改变。 [英] variable changed, but i didn't change is.

查看:84
本文介绍了变量发生了变化,但我没有改变。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我的程序中有下一个代码:


int main(无效)

{

加倍[6],x [5];

int i;


a [0] = 4 ;


printf(a [0] =%lf \ n \ nn,a [0]); // a [0] = 4.000000

for(i = 0; i< = 6; i ++)

{

x [i] = 6;

}


printf(" a [0] =%lf \ n \ n",a [0] ); // a [0] = 6.000000返回0;


}


如果我运行它,首先说a [0] = 4然后a [0] = 6.但是我没有b $ b改变a [0]。怎么可能,我做错了什么,或者我的计算机切换位是否有一个

鬼;)。

我在另一台计算机上编译它,得到了同样的问题。

Hi,

I do have the next code in my program:

int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

for (i=0;i<=6;i++)
{
x[i] = 6;
}

printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000 return 0;

}

If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn''t
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).
I compiled it on an other computer, and got the same problem.

推荐答案

Jasper Aikema< no ** @ none.none>潦草地写道:
Jasper Aikema <no**@none.none> scribbled the following:

我的程序中有下一个代码:
int main(void)
{
double a [6 ],x [5];
int i;

a [0] = 4;

printf(" a [0] =%lf \ n \ n,a [0]); // a [0] = 4.000000

for(i = 0; i< = 6; i ++)


仔细看看这一行。特别是结束条件。

{
x [i] = 6;
}

printf(" a [0] =%lf \\ \\ n \\ n \\ n,[0]); // a [0] = 6.000000返回0;
}
如果我运行它,首先是a [0] = 4,后来a [0] = 6.但是我没有改变[0]。怎么可能,我做错了什么,或者我的计算机切换位中是否有鬼魂;)。
我在另一台计算机上编译它,并遇到了同样的问题。
Hi, I do have the next code in my program: int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000

for (i=0;i<=6;i++)
Take a close look at this line. Particularly the ending condition.
{
x[i] = 6;
}

printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000 return 0; } If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn''t
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).
I compiled it on an other computer, and got the same problem.




您通过将6分配给x [5]和x [6]来创建未定义的行为。

在您的计算机上,这些似乎恰好与[3]和[4],

或[4]和[5]。或者可能有更复杂的解释。在

理论中,通过访问x [5]或x [6],你给了程序

允许把你的起居室涂成紫色。

没有违法行为,但你是否真的自己编写了这段代码,或者

是查找错误。做作业?


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)------ -------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

"''所谓的''意味着'''这有很长的解释,但我没有

时间在这里解释。''"

- JIPsoft



You are creating undefined behaviour by assigning 6 to x[5] and x[6].
On your computer, these seem to happen to coincide with a[3] and a[4],
or a[4] and a[5]. Or there might be a more complex explanation. In
theory, by accessing x[5] or x[6], you are giving the program
permission to paint your living room purple.
No offense intended, but did you really write this code yourself, or
is it a "find the error" homework exercise?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''So called'' means: ''There is a long explanation for this, but I have no
time to explain it here.''"
- JIPsoft


Jasper Aikema< no ** @ none.none>写道:
Jasper Aikema <no**@none.none> wrote:
我的程序中有下一个代码:
int main(void)
{
double a [6],x [5];
int i;

a [0] = 4;

printf(" a [0] =%lf \ n \ n",a [ 0]); // a [0] = 4.000000


不需要%lf在printf中 - %f也会这样做(你只需要它为扫描片()的
)。

for(i = 0; i< = 6; i ++)
{
x [i] = 6;
}


这里你写的是数组x的结尾(它有5个元素,

所以你可以使用的最大索引是4而不是6),从而调用un /
定义的行为,现在一切都会发生 - 包括醒来

在你的电脑里快乐地睡觉的幽灵;-)

printf(a [0] =%lf \ n \ nn,a [0]); // a [0] = 6.000000
返回0;
}
如果我运行它,首先是a [0] = 4,后来a [0] = 6.但是我没有't
更改[0]。怎么可能,我做错了什么,或者我的计算机切换位中是否有鬼魂;)。
I do have the next code in my program: int main(void)
{
double a[6],x[5];
int i;

a[0] = 4;

printf("a[0] = %lf\n\n", a[0]); // a[0] = 4.000000
No need for "%lf" in printf - "%f" will also do (you only need it
for scanf()).

for (i=0;i<=6;i++)
{
x[i] = 6;
}
Here you''re writing past the end of the array ''x'' (it has 5 elements,
so the largest index you can use is 4, not 6), thereby invoking un-
defined behaviour and now everything can happen - including waking
up the ghost that sleeps happily in your computer;-)
printf("a[0] = %lf\n\n", a[0]); // a[0] = 6.000000
return 0;
} If I run it, first is says a[0] = 4 and later a[0] = 6. But I didn''t
change a[0]. How is that possible, do I do something wrong or is there a
ghost in my computer switching bits ;).




形成外观似乎好像数组''a''存储在内存中

在数组''''之后,所以当你写''x''结束时你就开始了

覆盖''''的部分内容。但是不要依赖它 - 在不同的机器上或使用不同的编译器可能相当不同。


问候,Jens
-

\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de

\ __________________________ http://www.toerring.de



Form the look of it it seems as if array ''a'' is stored in memory
after array ''x'', so when you write past the end of ''x'' you start
to overwrite parts of ''a''. But don''t rely on that - it can be quite
different on a different machine or with a different compiler.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de




关于星期四, 2004年9月16日,Joona I Palaste写道:

On Thu, 16 Sep 2004, Joona I Palaste wrote:

Jasper Aikema< no ** @ none.none>潦草地写道:
[...]

Jasper Aikema <no**@none.none> scribbled the following: [...]
double a [6],x [5];
[...] for(i = 0; i< = 6; i ++)
double a[6],x[5]; [...] for (i=0;i<=6;i++)



仔细看看这一行。特别是结束条件。



Take a close look at this line. Particularly the ending condition.

{
x [i] = 6;
}
{
x[i] = 6;
}



您通过将6分配给x [5]和x [6]来创建未定义的行为。
在您的计算机上,这些似乎恰好与[3]和[4],
或[4]和[5]。


更有可能它们与[0]和[1]重合,是''a''的前两个元素。我不知道x [5]是如何与......同时的。 a [3]

除了在DS9000上!

或者可能有更复杂的解释。在
理论中,通过访问x [5]或x [6],您正在授予程序
允许将您的起居室涂成紫色的权利。


You are creating undefined behaviour by assigning 6 to x[5] and x[6].
On your computer, these seem to happen to coincide with a[3] and a[4],
or a[4] and a[5].
Much more likely they coincide with a[0] and a[1], the first two
elements of ''a''. I don''t see how x[5] could "coincide with" a[3]
except on the DS9000!
Or there might be a more complex explanation. In
theory, by accessing x[5] or x[6], you are giving the program
permission to paint your living room purple.




确实。


HTH,

-Arthur



Indeed.

HTH,
-Arthur


这篇关于变量发生了变化,但我没有改变。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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