0

Background: the overall program is designed to carry out 2D DIC between a refference image and 1800 target images, (for tomographic reconstruction) In my code, there is this for loop block

for (k=0; k<kmax; k++)
{
    K=nm12+(k*(h-n+1))/(kmax-1);
    printf("\nk=%d\nL= ", K);
    for (l=0; l<lmax; l++)
    {
        ///For each subset, calculate and store its mean and standard deviation.   
        ///Also want to know the sum and sum of squares of subset, but in two sections, stored in fm/df[k][l][0 and 1].
        L=nm12+(l*(w-n+1))/(lmax-1);
        printf("%d ", L);
        fm[k][l][0]=0;
        df[k][l][0]=0;
        fm[k][l][1]=0;
        df[k][l][1]=0;
        ///loops are j then i as it is more efficient (saves m-1 recalculations of b=j+L; 
        for (j=0; j<m; j++)
        {
            b=j+L;
            for (i=0; i<M; i++)
            {
                a=i+K;
                fm[k][l][0]+=ref[a][b];
                df[k][l][0]+=ref[a][b]*ref[a][b];
            }

            for (i=M; i<m; i++)
            {
                a=i+K;
                fm[k][l][1]+=ref[a][b];
                df[k][l][1]+=ref[a][b]*ref[a][b];
            }
        }
        fm[k][l][2]=m2r*(fm[k][l][1]+fm[k][l][0]);
        df[k][l][2]=sqrt(df[k][l][1]+df[k][l][0]-m2*fm[k][l][2]*fm[k][l][2]);
        a+=1;
    }
}

Each time l reaches 10 the line df[k][l][2]=sqrt(df[k][l][1]+df[k][l][0]-m2*fm[k][l][2]*fm[k][l][2]); appears to no longer be executed. By this I mean the debugger shows that the value of df[k][l][2] is not changed from zero to the sum correctly. Also, df[k][l][0 and 1] remain fixed regardless of k and l, just as long as l>=10.

kmax=15, lmax=20, n=121, m=21, M=(3*m)/4=15, nm12=(n-m+1)/2=50.

The arrays fm and df are double arrays, declared double fm[kmax][lmax][3], df[kmax][lmax][3];

Also, the line a+=1; is just there to be used as a breakpoint to check the value of df[k][l][2], and has no affect on the code functionality.

Any help as to why this is happening, how to fix, etc will be muchly appreciated!

EDIT: MORE INFO.

The array ref (containing the reference image pixel values) is a dynamic array, with memory allocated using malloc, in this code block:

    double **dark, **flat, **ref, **target, **target2, ***gm, ***dg;
    dark=(double**)malloc(h * sizeof(double*));
    flat=(double**)malloc(h * sizeof(double*));
    ref=(double**)malloc(h * sizeof(double*));
    target=(double**)malloc(h * sizeof(double*));
    target2=(double**)malloc(h * sizeof(double*));
    size_t wd=w*sizeof(double);
    for (a=0; a<h; a++)
    {
        dark[a]=(double*)malloc(wd);
        flat[a]=(double*)malloc(wd);
        ref[a]=(double*)malloc(wd);
        target[a]=(double*)malloc(wd);
        target2[a]=(double*)malloc(wd);
    }

where h=1040 and w=1388 the dimensions of the image.

8
  • Looks like this may not be enough code to show the cause. What happens when you step through in the debugger? Also, note that "implemented" is not the same thing as "executed"... you seem to have meant executed in your question title. Commented Aug 14, 2013 at 11:36
  • Ok, so it looks like the problem is not that line, but earlier on, as for l>=10, fm[k][l][2]=1616 always, and the values of df[k][l][0 and 1] are 822608640 and 329043456, the three of which actually lead to a correct value of df[k][l][2]=0. Commented Aug 14, 2013 at 12:00
  • readability is really an issue here. Can't read it easily with all those arrays. Commented Aug 14, 2013 at 13:57
  • @TonyTheLion - would have liked to see the 'Lightness' version of your comment :) Commented Aug 14, 2013 at 14:12
  • -{as requested}- Holy frak that's a lot of subscripts and asterisks. Your first task is to clean that up so that your code is actually legible... since, right now, I'd rather gouge out my eyes with a Lisp parenthesis. (A few code comments wouldn't exactly hurt, either!) Commented Aug 14, 2013 at 14:15

1 Answer 1

2

You don't mention much about what compiler, IDE or framework that you're using. But a way to isolate the problem is to create a new small (console) project, containing only the snippet you've posted. This way you'll eliminate most kinds of input/thread/stack/memory/compiler etc. issues.
And if it doesn't, it'll be small enough to post the whole sample here on stackoverflow, for us take apart and ponder.

Ergo you should create a self contained unit test for your algorithm.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok having done that, it seems to work fine, so it is probably a memory issue with the program as a whole...
Increase your stack size
Previously, all errors related to stack size have caused the execution of the code to terminate, which is why I have had to use malloc for a lot of the arrays... but I'll look into it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.