2D array pointers = mem leak; help!

Joined
Mar 31, 2010
Messages
2
Reaction score
0
Hello all! I'm having a memory leak issue, somewhere I'm corrupting memory and it's causing my little QR factorization program to fail on a printf() call. I've stripped the program down to the following malloc/free skeleton and even in this form it causes the seg fault on recursive call #508. Valgrind says I have no memory leaks. Electric Fence just stops the program on the printf. I must be mishandling my 2D arrays (I want to use pointers like this because the size of the 2D array that gets passed to recursion() changes every time). Any ideas? Thanks very much for any help! Oh - for smaller matrices this works, for what that;s worth. And to reiterate, the code here is stripped down so it doesn't do anything useful, but it's still seg faulting.

int recursionLevel = 0, printOut = 0;
void recursive(double **A, double **Q, double **R, int m, int n);

int main(int argc, char *argv[])
{
int i, j, k, m, n, numSamples = 0;
double **A, **Q, **R;

m = 2048;
n = 1024;

// malloc matrices (A and Q are m by n, R is n by n)
A = (double**)malloc(sizeof(double*) * m);
if (A == NULL)
{
printf("Malloc failed\n");
exit(1);​
}
Q = (double**)malloc(sizeof(double*) * m);
if (Q == NULL)
{
printf("Malloc failed\n");
exit(1);​
}
R = (double**)malloc(sizeof(double*) * n);
if (R == NULL)
{
printf("Malloc failed\n");
exit(1);​
}
for (i = 0; i < m; i++)
{
A = (double*)malloc(sizeof(double) * n);
if (A == NULL)
{
printf("Malloc failed\n");
exit(1);​
}
Q = (double*)malloc(sizeof(double) * n);
if (Q == NULL)
{
printf("Malloc failed\n");
exit(1);​
}
if (i < n)
{
R = (double*)malloc(sizeof(double) * n);
if (R == NULL)
{
printf("Malloc failed\n");
exit(1);​
}

}

}

// load A (real program reads binary file but this placeholder still fails)
for (i = 0; i < n; i++)
{

for (j = 0; j < m; j++)
{
A[j] = 666; // filling column by column

}

}

// call recursion
recursive(A, Q, R, m, n);

// de-allocate matrices
for (i = 0; i < m; i++)
{
free(Q);
if (i < n)
{
free(R);

}

}
free(Q);
free(R);

} // end main

void recursive(double **A, double **Q, double **R, int m, int n)
{
// declare vars
double a1[m], **A2;
int i, j;
printf("%d\n", recursionLevel); // SEG FAULTING HERE ON CALL #508

// run following block unless this is last recursion call (width of A
// reduced by one column every time until it's just one column wide)

if (n != 1)
{
// malloc A2
A2 = (double**)malloc(sizeof(double*) * m);
if (A2 == NULL)
{
printf("Malloc failed\n");
exit(1);​
}
for (i = 0; i < m; i++)
{
A2 = (double*)malloc(sizeof(double) * (n - 1));
if (A2 == NULL)
{
printf("Malloc failed\n");
exit(1);​
}

}

// generate a1 and A2
// a1 is first column of A, A2 is remaining columns
// even if we don't write values into a1 and A2 this seg faults

for (i = 0; i < m; i++)
{
// no code here in skeleton; still seg faults
free(A);

}

// free pointers
free(A);

// recursive call
recursionLevel++;
recursive(A2, Q, R, m, n - 1);

}
else
{
// never get here because I'm segfaulting on recursion level 508
}

} // end recursive
 
Last edited:
Joined
Mar 31, 2010
Messages
2
Reaction score
0
For anyone interested I've located the problem. I didn't think of this as a memory leak because it's not a malloc family call (not explicitly), but each time I run the recursion I declare "double a1[m]" and these of course aren't freed until all the recursions are finished. They just keep piling up and cause an overflow. I've changed a1 to a double* that I malloc and free before calling the next recursion and that has fixed the seg fault.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top