I'm getting problems trying to find out what I'm doing wrong in my code, because valgrind is telling me that my code generates a read error.
This is the problematic function:
double func3(double *xp, double *yp, int dims, double *u_b,
double *array, int nc)
{
int i, j, pos;
double cx, cy, dx, dy, dist, rx, ry, sx, sy, sxy, trxx, tryy, trxy,ans;
//printf("size of n:", dims);
sx = sy = sxy = trxy = 0.0;
for (j = 0; j < dims; j++) {
rxx = ryy = 0.0;
for (i = 0; i < dims; i++) {
if (i != j) {
dx = (xp[i] - xp[j]);
dy = (yp[i] - yp[j]);
dist = calc(dx, dy);
pos = find(u_b, nc, dist);
cx = array[pos];
//printf("i,j: %i , %i\n",i,j);
//printf("\t xposi = %f\t\t xposj= %f\n",xp[i],xp[j]);
//printf("\t yposi = %f\t\t yposj= %f\n",yp[i],yp[j]);
//printf("\t dx = %f\t\t dy= %f\n",dx,dy);
//printf("\t value of pos: %i value of nc: %i\n", pos, nc);
/*problematic line*/
cy = array[pos + nc];
printf("\t cx: %f\t\t cy: %f\n",cx,cy);
printf("\n");
}
else
cx = cy = 1.0;
rx += cx;
ry += cy;
trxy += cx * cy;
}
sx += rx;
sy += ry;
sxy += rx * ry;
}
trxx = (double) dims - sx / dims;
tryy = (double) dims - sy / dims;
trxy += (sx * sy / dims - 2.0 * sxy) / dims;
ans = trxx * tryy / trxy - 1.0;
return ans;
}
So the error traceback from valgrind is:
==22457== Invalid read of size 8
==22457== at 0xFBC3076: func3 (funcs.c:90)
==22457== by 0xFBC3204: func2 (funcs.c:121)
==22457== by 0xFBC3335: func1 (funcs.c:11)
==22457== by 0x485A53: do_dotCode (dotcode.c:1722)
==22457== by 0x4BACF3: Rf_eval.part.13 (eval.c:651)
==22457== by 0x4BDC6D: do_set (eval.c:1991)
==22457== by 0x4BAB5B: Rf_eval.part.13 (eval.c:623)
==22457== by 0x4BDE93: do_begin (eval.c:1620)
==22457== by 0x4BAB5B: Rf_eval.part.13 (eval.c:623)
==22457== by 0x4BF40C: Rf_applyClosure (eval.c:1033)
==22457== by 0x4BA907: Rf_eval.part.13 (eval.c:670)
==22457== by 0x4BDC6D: do_set (eval.c:1991)
==22457== Address 0xd68c758 is 0 bytes after a block of size 248 alloc'd
==22457== at 0x4A0887C: malloc (vg_replace_malloc.c:270)
==22457== by 0x4E9750: Rf_allocVector (memory.c:2465)
==22457== by 0x44790E: do_makevector (builtin.c:756)
==22457== by 0x4B0595: bcEval (eval.c:4700)
==22457== by 0x4BA83F: Rf_eval.part.13 (eval.c:554)
==22457== by 0x4BF40C: Rf_applyClosure (eval.c:1033)
==22457== by 0x4BA907: Rf_eval.part.13 (eval.c:670)
==22457== by 0x4BC3E1: Rf_evalList (eval.c:2081)
==22457== by 0x4BAC0C: Rf_eval.part.13 (eval.c:642)
==22457== by 0x4BDC6D: do_set (eval.c:1991)
==22457== by 0x4BAB5B: Rf_eval.part.13 (eval.c:623)
==22457== by 0x4BDE93: do_begin (eval.c:1620)
==22457==
So the program doesnt seg fault, in fact if I uncomment the "prints" on the code I can see that the error is found at random values for the loop indexes (i,j) and it occurs when the code tries to read from "array[pos+nc]" but it actually reads the correct value at that position!!! (for the corresponding values of pos and nc) a number of times before de valgrind error and after the valgrind error and at the error... so I'm completly lost here.
I check that the arrays "array", "xp" and "yp" were correctly filled at all the required entries, and the other functions were checked as well.
Any help will be much appreciated.
31doubles. Is that really the size you expected yourarray[]to be? - woolstarcy = array[pos + nc];To me it looks like nc is the number of elements in array, in which casearray[pos + nc];would be off by one, indeed. - wildplasser