同样的程序不同的优化级别程序运行结果竟然不一样甚至出错,而且出错了极难找到原因,郁闷中~~不知有谁遇到过类似情况? 今天发现做的一批产品中,同样的软件,同样的硬件,唯一不同的只是硬件序列号,竟然有些产品运行到某些地方的时候莫名其妙出错,找了半天原因最终锁定出错于以下代码: void activation_1(void){ uchar snx[8]={0,0,0,0,0,0,0,0}; uint code0,code1,code2,code3; uchar i,k; LCM_cls(); showsn(); showstr(0,12,"Code:"); showchar(5,12,'0'); code0=(uint) val_input(0,24,6,5,0,0); showchar(5,12,'1'); code1=(uint) val_input(0,24,6,5,0,0); showchar(5,12,'2'); code2=(uint) val_input(0,24,6,5,0,0); showchar(5,12,'3'); code3=(uint) val_input(0,24,6,5,0,0); snx[0]=code0>>8; snx[1]=code0&0xff; snx[2]=code1>>8; snx[3]=code1&0xff; snx[4]=code2>>8; snx[5]=code2&0xff; snx[6]=code3>>8; snx[7]=code3&0xff; k=1; for(i=0;i<8;i++) { if(snx!=randomx[sn^manufacturer]) k=0; } if(k==0) { delay(5000); poweroff(); while(1); }else { for(i=0;i<8;i++) EEPROM_write(i+0xab,snx); EEPROM_write(0xa3,'Y'); EEPROM_write(0xa4,'S'); EEPROM_write(0xa5,0x00); EEPROM_write(0xa6,0x00); setchecksum(); } }
void activation_2(void){ uchar snx[8]={0,0,0,0,0,0,0,0}; uint code0,code1,code2,code3; uchar i,k; LCM_cls(); showsn(); showstr(0,12,"Code:"); showchar(5,12,'0'); code0=(uint) val_input(0,24,6,5,0,0); showchar(5,12,'1'); code1=(uint) val_input(0,24,6,5,0,0); showchar(5,12,'2'); code2=(uint) val_input(0,24,6,5,0,0); showchar(5,12,'3'); code3=(uint) val_input(0,24,6,5,0,0); snx[0]=code0>>8; snx[1]=code0&0xff; snx[2]=code1>>8; snx[3]=code1&0xff; snx[4]=code2>>8; snx[5]=code2&0xff; snx[6]=code3>>8; snx[7]=code3&0xff; k=1; for(i=0;i<8;i++) { if(snx!=randomx[sn^brand]) k=0; } if(k==0) { delay(5000); poweroff(); while(1); }else { EEPROM_write(0xa5,0xaa); EEPROM_write(0xa6,0x00); setchecksum(); } }
这两个函数基本一样,但问题就出函数activation_2运行到最后的EEPROM_write(0xa5,0xaa);这一行时竟然跳到函数activation_1里去了!! 后来把这两个函数改为: void activation_1(void){ uchar snx[8]={0,0,0,0,0,0,0,0}; uint code0,code1,code2,code3; uchar i,k; LCM_cls(); showsn(); showstr(0,12,"Code A:"); showchar(7,12,'0'); code0=(uint) val_input(0,24,6,5,0,0); showchar(7,12,'1'); code1=(uint) val_input(0,24,6,5,0,0); showchar(7,12,'2'); code2=(uint) val_input(0,24,6,5,0,0); showchar(7,12,'3'); code3=(uint) val_input(0,24,6,5,0,0); snx[0]=code0>>8; snx[1]=code0&0xff; snx[2]=code1>>8; snx[3]=code1&0xff; snx[4]=code2>>8; snx[5]=code2&0xff; snx[6]=code3>>8; snx[7]=code3&0xff; k=1; for(i=0;i<8;i++) { if(snx!=randomx[sn^manufacturer]) k=0; } if(k==0) { delay(5000); poweroff(); while(1); }else { for(i=0;i<8;i++) EEPROM_write(i+0xab,snx); EEPROM_write(0xa3,'Y'); EEPROM_write(0xa4,'S'); EEPROM_write(0xa5,0x00); EEPROM_write(0xa6,0x00); setchecksum(); } }
void acti(void){ EEPROM_write(0xa6,0x00); EEPROM_write(0xa5,0xaa); setchecksum(); }
void pwo(void){ delay(4000); poweroff(); for(;;);
}
void activation_2(void){ uchar snx1[8]={0,0,0,0,0,0,0,0}; uint code0,code1,code2,code3; uchar i,k; LCM_cls(); showsn(); showstr(0,12,"Code B:"); showchar(7,12,'0'); code0=(uint) val_input(0,24,6,5,0,0); showchar(7,12,'1'); code1=(uint) val_input(0,24,6,5,0,0); showchar(7,12,'2'); code2=(uint) val_input(0,24,6,5,0,0); showchar(7,12,'3'); code3=(uint) val_input(0,24,6,5,0,0); snx1[0]=code0>>8; snx1[1]=code0&0xff; snx1[2]=code1>>8; snx1[3]=code1&0xff; snx1[4]=code2>>8; snx1[5]=code2&0xff; snx1[6]=code3>>8; snx1[7]=code3&0xff; k=1; for(i=0;i<8;i++) { if(snx1!=randomx[sn^brand]) k=0; } if(k==0) { pwo(); }else { acti(); } } ,即把activation_2最后的部分写成独立的函数,才能正确运行。请问各位大侠,不知这如何解释? |