由于陈述的问题,我对我的程序感到困惑。当我在为数组分配计算值之前或之后放置printf语句时,我得到了正确的答案。否则,这是一个奇怪的数字。这个数字非常一致。 printf不必包含与数组相关的任何内容。我检查了这个answer,但没有得到很多。以下是我的代码的一部分:
int xcorr(Imdata, Kernel, sizeIm, sizeKernel, result_im)
unsigned short** Imdata;
unsigned short** Kernel;
long sizeIm[2], sizeKernel[2];
float** result_im;
{
register float Imstd, Imavg, Kernelstd, Kernelavg, combine_avg, outtmp, area;
register int bufYsize, bufXsize;
int i,j,r,c;
register unsigned short *ImdataPtr, *KernelPtr;
register float *result;
register float val1, val2;
// kernel average and standard deviation are constant throughout this function
area = sizeKernel[0]*sizeKernel[1];
outtmp = Kernelavg = 0;
// Using the One-pass standard variance algorithm
for(i=0;i<sizeKernel[0];i++){
KernelPtr = Kernel[i];
for(j=0;j<sizeKernel[1];j++){
val1 = KernelPtr[j];
Kernelavg += val1;
outtmp += val1*val1;
}
}
Kernelavg /= area;
outtmp /= area;
// now the standard deviation
Kernelstd = sqrt(outtmp - Kernelavg*Kernelavg);
/////////////////////////////////////////////////
// Perform Correlation
/////////////////////////////////////////////////
bufYsize = sizeIm[0] - sizeKernel[0] + 1;
bufXsize = sizeIm[1] - sizeKernel[1] + 1;
for(i=0;i<bufYsize;i++){
result = result_im[i];
for(j=0;j<bufXsize;j++){
combine_avg = outtmp = Imavg = 0;
// Using one-pass method for standard variance:
for (r = 0; r < sizeKernel[0]; r++){
KernelPtr = Kernel[r];
ImdataPtr = Imdata[i+r];
for (c = 0; c < sizeKernel[1]; c++){
val1 = KernelPtr[c];
val2 = ImdataPtr[j+c];
Imavg += val2;
outtmp += val2*val2;
combine_avg += val1*val2;
}
}
outtmp /= area;
Imavg /= area;
// standard deviation
if((Imstd = outtmp - Imavg*Imavg)<=SMALL_VAR){
result[j] = 0;
continue;
}
Imstd = sqrt(outtmp - Imavg*Imavg);
combine_avg /= area;
result[j] = (float)combine_avg-(Imavg*Kernelavg))/(Kernelstd*Imstd);
}
}
return 0;
}
错误与结果有关,该结果在main中分配如下:
result = (float**)malloc(bufYsize*sizeof(float*));
if(result == NULL){
printf("Error allocating the output array (pair num=%d)\n",k+1);
xit_status = 1; //exit(EXIT_FAILURE);
}
else{
for(i=0;i<bufYsize;i++){
result[i]=malloc(bufXsize*sizeof(float));
if(result[i]==NULL){
printf("Error allocating the output array at row %d, pair %d\n",i,k+1);
exit_status = 1; //exit(EXIT_FAILURE);
}
}
}
答案 0 :(得分:1)
printf
应该能够影响变量的唯一方法是通过%n
格式说明符。
int x;
printf("foo%nbar\n", &x);
printf("%d\n", x); // 3
如果正在修改其他任何内容,那么您的程序必须在某处调用未定义的行为。我建议从Valgrind(或类似的)开始追踪它。