[英]why does array initialization in function other than main is temporary?
This is the code in which i initialize array "turn[20]" in main as well as in function "checkCollisionOrFood()",the four values turn[0],turn[1],turn[2],turn[3] are initialized to zero in main function,rest are being intialized in "checkCollisionOrFood()".This is where fault starts.when i initialize turn[4]=0 in "checkCollisionOrFood()" and then access it anywhere,it remains 0 in any function,but! 这是我在main和函数“ checkCollisionOrFood()”中初始化数组“ turn [20]”的代码,四个值分别为turn [0],turn [1],turn [2],turn [3]在主函数中初始化为零,其余部分在“ checkCollisionOrFood()”中初始化。这是故障开始的地方。当我在“ checkCollisionOrFood()”中初始化turn [4] = 0并在任何地方访问它时,它保持为0。任何功能,但是! when i initialize next turn[] ie turn[5],the value of turn[4] gets depleted .ie turn[4] have garbage value.turn[20] is global variable,its index"head" is also global.I'm stuck with this problem plz help me get out of it.This is my complete code
当我初始化下一个turn []即turn [5]时,turn [4]的值将耗尽。即turn [4]具有垃圾值。turn[20]是全局变量,其索引“ head”也是全局的。卡住了这个问题,请帮助我摆脱困境。这是我完整的代码
#include <mega8.h>
#include <delay.h>
#include <stdlib.h>
unsigned short address[]= {0b00001111,0b00000000,0b00000000,0b00000000,
0b00000000,0b00000000,0b00000000,0b00000000};
unsigned short turn[20];//it is declared as global variable.
unsigned short head=3;
unsigned short colHead=0;
unsigned short rowHead =3;
unsigned short rowTail=0;
unsigned short colTail=0;
unsigned short size=4;
unsigned short foodx;
unsigned short foody;
...
unsigned short checkCollisionOrFood()
{
head=(head+1)%20;
if(turn[head-1]==0){
turn[head]=0;
/ this is where turn[] is iniliazized and if i access turn[head] here ie just after iniliazition then it gives correct value but if i access its previous value means turn[head-1]then it gives garbage value / / 这是turn []被初始化的地方,如果我在此处访问turn [head],即在初始化之后,它会给出正确的值,但是如果我访问其先前的值意味着turn [head-1],那么它将给出垃圾值 /
PORTB=255;PORTB&=~(1<<turn[4]);PORTD.7=0;delay_ms(200);PORTD.7=1;
/ by this statement i checked the value of turn[4] just after initilization of turn[4] which gives right answer and then after initialization of turn[5] and accessing turn[4] gives garbage. / 通过此语句,我在turn [4]初始化后立即检查了turn [4]的值,该值给出了正确的答案,然后在turn [5]初始化并访问turn [4]后产生了垃圾。 /
/
rowHead=(rowHead+1)%8;
if(!(address[colHead]&(1<<rowHead)))return 1;
else if((address[colHead]&(1<<rowHead))&&
(!((colHead==foody)&&(rowHead==foodx))))gameOver();
else return 0;
}
if(turn[head-1]==1){
turn[head]=1;
colHead=(colHead+1)%8;
if(!(address[colHead]&(1<<rowHead)))return 1;
else if((address[colHead]&(1<<rowHead))&&
(!((colHead==foody)&&(rowHead==foodx))))gameOver();
else return 0;
}
}
void main(void)
{
turn[0]=0;turn[1]=0;turn[2]=0;turn[3]=0;
/ these values of turn[] are not changed irrespective of where they are accessed. / 不论在何处访问,turn []的这些值都不会更改。 /
/
while (1)
{
if(checkCollisionOrFood())
{
PORTB=(address[colHead] |=1<<rowHead);
turnOffTail();
blink();
}
else
{
PORTB=address[colHead];
createFood();
blink();
}
}
} }
Plz suggest me to get out of it. Plz建议我摆脱它。
If you observer garbage in head
this could easily due to accessing address
out of bounds, as this is located in memrory directly before turn
如果你观察垃圾
head
这很容易由于访问address
出界,因为这个位于memrory直接前turn
Check the indexing of address
to make sure you do not write beyond it's boundaries and with this overwrite data in the next variable, that is turn
. 检查
address
的索引,以确保您不会写超出其范围的内容,并使用下一个变量turn
中的此数据进行覆盖。
unsigned short checkCollisionOrFood()
{
head=(head+1)%20;
if(turn[head-1]==0){
turn[head]=0;
You might have a problem when "head" is reaching "20". 当“ head”达到“ 20”时,您可能会遇到问题。 The value of "head" is then rolling back to 0, which leads you to test the value of "turn[-1]".
然后,“ head”的值回滚为0,这将导致您测试“ turn [-1]”的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.