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[])
{
} // end main
void recursive(double **A, double **Q, double **R, int m, int n)
{
} // end recursive
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)
{
Q = (double**)malloc(sizeof(double*) * m);
if (Q == NULL)
{
R = (double**)malloc(sizeof(double*) * n);
if (R == NULL)
{
for (i = 0; i < m; i++)
{
}
// load A (real program reads binary file but this placeholder still fails)
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
}
}
// call recursion
recursive(A, Q, R, m, n);
// de-allocate matrices
for (i = 0; i < m; i++)
{
}
free(Q);
free(R);
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);
}exit(1);
Q = (double**)malloc(sizeof(double*) * m);
if (Q == NULL)
{
printf("Malloc failed\n");
exit(1);
}exit(1);
R = (double**)malloc(sizeof(double*) * n);
if (R == NULL)
{
printf("Malloc failed\n");
exit(1);
}exit(1);
for (i = 0; i < m; i++)
{
A = (double*)malloc(sizeof(double) * n);
if (A == NULL)
{
Q = (double*)malloc(sizeof(double) * n);
if (Q == NULL)
{
if (i < n)
{
}
if (A == NULL)
{
printf("Malloc failed\n");
exit(1);
}exit(1);
Q = (double*)malloc(sizeof(double) * n);
if (Q == NULL)
{
printf("Malloc failed\n");
exit(1);
}exit(1);
if (i < n)
{
R = (double*)malloc(sizeof(double) * n);
if (R == NULL)
{
if (R == NULL)
{
printf("Malloc failed\n");
exit(1);
}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)
{
}
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)
}
else
{
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)
{
for (i = 0; i < m; i++)
{
}
// 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++)
{
}
// free pointers
free(A);
// recursive call
recursionLevel++;
recursive(A2, Q, R, m, n - 1);
// malloc A2
A2 = (double**)malloc(sizeof(double*) * m);
if (A2 == NULL)
{
printf("Malloc failed\n");
exit(1);
}exit(1);
for (i = 0; i < m; i++)
{
A2 = (double*)malloc(sizeof(double) * (n - 1));
if (A2 == NULL)
{
if (A2 == NULL)
{
printf("Malloc failed\n");
exit(1);
}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(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: