记得有次编程中忘了初始化变量,这导致了计算结果不正确,debug后发现未始化造成的,现记录此问题也让大家不要再进坑了!
上代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
double Temp1,Temp2; double TotalAb,TotalAA ,TotalBB ; for(int i=0;i<Height1; ++i) { ????? for(int j=0;j<Width1 ; ++j) ????? { ????????? Temp1 = TempImageDataPtr1[j]-ImageAverage1; ????????? Temp2 = TempImageDataPtr2[j]-ImageAverage2; ????????? TotalAb += Temp1*Temp2; ????????? TotalAA += Temp1*Temp1; ????????? TotalBB += Temp2*Temp2; ????? } ????? TempImageDataPtr1 += ImagePitch1; ????? TempImageDataPtr2 += ImagePitch2; } |
debug发现TotalAA值很大(7.9434E+125)不对,后设断点一句句运行,发现声明完未始化造成.
改成如下,结果正确:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
double Temp1=0; double Temp2=0; double TotalAb = 0; double TotalAA = 0; double TotalBB = 0; for(int i=0;i<Height1; ++i) { ????? for(int j=0;j<Width1 ; ++j) ????? { ????????? Temp1 = TempImageDataPtr1[j]-ImageAverage1; ????????? Temp2 = TempImageDataPtr2[j]-ImageAverage2; ????????? TotalAb += Temp1*Temp2; ????????? TotalAA += Temp1*Temp1; ????????? TotalBB += Temp2*Temp2; ????? } ????? TempImageDataPtr1 += ImagePitch1; ????? TempImageDataPtr2 += ImagePitch2; } |
看来养成良好的编程习惯也是编程的一部份。
上次我也碰到这个问题,debug好久才找到先收藏了
Appreciation to my father who stated to me on the topic of this webpage, this web site is truly awesome.