1
votes

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.

1
This looks like an off-by-one error. - user529758
What were the parameters of the run where the error was reported? valgrind is telling you that the block allocated was only 31 doubles. Is that really the size you expected your array[] to be? - woolstar
As I said to Sean... I debug the code and the values of the parameters when the read error is found are pos=8 and nc=10. The problem is that array[18] has been read multiple times before the error and retrieves correctly the value... in fact at the moment of the error also the value retrieves from array is correct - user20679
@wooltar: the sizes are fine, I checked the values on them... the program actually reads the values correctly, but valgrind complains... i dont know why - user20679
cy = array[pos + nc]; To me it looks like nc is the number of elements in array, in which case array[pos + nc]; would be off by one, indeed. - wildplasser

1 Answers

0
votes

To expound on @H2CO3's comment find() may not be giving you the value you expect or nc is incorrect. Fire up a debugger and inspect the situation.