What are some scenario where printf() would affect a variable in c? -
i stuck program because of stated problem. when put printf statement before or after assigning computed value array, correct answer. otherwise, it's strange number. number pretty consistent though. printf not have contain relevant array. check answer, did not out of it. below part of code:
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 , standard deviation constant throughout function area = sizekernel[0]*sizekernel[1]; outtmp = kernelavg = 0; // using 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; // 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 standard variance: (r = 0; r < sizekernel[0]; r++){ kernelptr = kernel[r]; imdataptr = imdata[i+r]; (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; }
the error result, allocated in main follow:
result = (float**)malloc(bufysize*sizeof(float*)); if(result == null){ printf("error allocating 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 output array @ row %d, pair %d\n",i,k+1); exit_status = 1; //exit(exit_failure); } } }
the way printf
should able affect variable via %n
format specifier.
int x; printf("foo%nbar\n", &x); printf("%d\n", x); // 3
if else being modified, program must invoking undefined behaviour somewhere. suggest starting valgrind (or similar) track down.
Comments
Post a Comment