Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to free allocated memory
    text
    copied!<p>I'm a beginner in C and I have this problem where I <code>malloc/calloc</code> two-dimensional arrays and get errors when trying to <code>free</code> them after use. The programs fills square matrices with random values and lets the user choose between different functions to multiply them, in order to evaluate performance. Here is the code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/time.h&gt; #include &lt;cblas.h&gt; #include &lt;assignment2.h&gt; // declares my non-blas multiplication functions int main(int argc, const char *argv[]) { int n, i, j, blockSize, choice; printf("Enter n: "); scanf("%d", &amp;n); printf("Enter block size: "); scanf("%d", &amp;blockSize); do { printf("Enter method (1 -&gt; naive, 2 -&gt; ijk-blocked, " "3 -&gt; kij-blocked, 4 -&gt; cblas_dgemm): "); scanf("%d", &amp;choice); } while (choice &lt; 1 || choice &gt; 4); double **a, **b, **result; /* Init matrices*/ a = malloc(n * sizeof(double*)); b = malloc(n * sizeof(double*)); result = malloc(n * sizeof(double*)); if (a == NULL || b == NULL || result == NULL) { printf("Error.\n"); return 1; } for (i = 0; i &lt; n; i++) { *(a + i) = malloc(n* sizeof(double)); *(b + i) = malloc(n* sizeof(double)); *(result + i) = calloc(n, sizeof(double)); } fillMatrix(n, a); fillMatrix(n, b); // timing struct timeval tv1, tv2; struct timezone tz; gettimeofday(&amp;tv1, &amp;tz); switch(choice) { case 1: printf("matmul_ijk\n"); matmul_ijk(n, a, b, result); break; case 2: printf("matmul_ijk_blocked\n"); matmul_ijk_blocked(n, blockSize, a, b, result); break; case 3: printf("matmul_kij_blocked\n"); matmul_kij_blocked(n, blockSize, a, b, result); break; case 4: printf("cblas_dgemm\n"); cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, n, n, n, 1.0, a[0], n, b[0], n, 0.0, result[0], n); break; default: puts("Error. Mode not recognized."); } gettimeofday(&amp;tv2, &amp;tz); // Print time double elapsed = (double) (tv2.tv_sec-tv1.tv_sec) + (double) (tv2.tv_usec-tv1.tv_usec) * 1.e-6; printf("Time elapsed: %lf\n", elapsed); // for (i = 0; i &lt; n; i++) { // free(a[i]); // free(b[i]); // free(result[i]); // } // free(a); // free(b); // free(result); return 0; } </code></pre> <p>Now at the very end I commented out my attempts to free the memory I had previously allocated. Because if I enable the last three calls to <code>free</code>, I'm getting errors of the sort </p> <pre><code>2(724,0x7fff7cc76960) malloc: *** error for object 0x7fe62a016000: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug </code></pre> <p>and I don't know why, since I did allocate the memory myself. But the program runs normally and delivers the result, it's only on freeing that the error happens. Interestingly, this happens <b>only</b> when <code>n</code> is large. For e.g. <code>n=1000</code> there is no issue, but for <code>n=2000</code> there is. For speed reasons I always choose the <code>BLAS</code>routine to test this, I don't know if the others display the same behaviour. </p> <p>So I'm guessing I'm missing something important here since </p> <ol> <li>Memory <code>malloc</code>ed by me does not seem to want to be <code>free</code>d</li> <li>The problem appears to be related to the size of the matrix and I don't get how.</li> </ol> <p>Can anyone point out my mistake?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload