Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction not free-ing in time
    primarykey
    data
    text
    <p>I've had a look at a few other questions on SO but none of them seem to address a similar problem. </p> <p>I have a function which sorts an array (using heap sort) and calculates the median. The heap sort routine has been taken directly from Numerical Recipies. </p> <p>I am <code>calloc</code>ing and <code>free</code>ing an array inside the median function, but <code>free</code> doesn't seem to be freeing up the space in time. Here's some code to illustrate what I mean :</p> <pre><code>int calcMedian(int n1, int n2, int *dat) { int ii, npt; int *inparr, retval; npt = n2 - n1 + 1; /* Number of elements in array */ inparr = calloc(npt+1, sizeof(*inparr)); for(ii = n1; ii &lt;= n2; ii++) inparr[ii-n1+1] = dat[ii]; /* ii-n1+1 because heapsort function likes arrays to start from 1 */ heapsortInt(npt, inparr); /* The error isn't here, function has been previously debugged. Sorting is in-place.*/ if (npt % 2) retval = inparr[(npt+1)/2]; else retval = (inparr[npt/2]+inparr[npt/2+1])/2; free(inparr); return(retval); } </code></pre> <p>The function <code>heapsortInt</code> has been quite thoroughly debugged and has been used in several other places without issue. Now I call my function <code>calcMedian</code> in a loop like so :</p> <pre><code>for(ii = 0; ii &lt; maxval; ii++) { index = ii * maxpt; med1 = calcMedian(index, index+npt1[ii]-1, data1+index); med2 = calcMedian(index, index+npt2[ii]-1, data2+index); } </code></pre> <p>where the relevant variables are defined below :</p> <pre><code>int *data1, *data2; int *npt1, *npt2; data1 = calloc(maxval * maxpt, sizeof(*data1)); data2 = calloc(maxval * maxpt, sizeof(*data2)); npt1 = calloc(maxval, sizeof(*npt1)); npt2 = calloc(maxval, sizeof(*npt2)); </code></pre> <p>So I'm basically passing different sections of one large array into <code>calcMedian</code> and getting back the necessary median values. </p> <p><strong>THE PROBLEM:</strong> <code>calcMedian</code> seems to be crashing when it hits the second function call. I ran it through valgrind, and this is what it told me:</p> <pre><code>Invalid read of size 4 at 0x43F67E: calcMedian /* Line no. pointing to calloc in calcMedian */ by 0x4416C9: main /* Line no pointing to second call of calcMedian */ Address 0x128ffdc0 is 6,128 bytes inside a block of size 110,788 free'd at 0x4A063F0: free by 0x43F728: calcMedian /* Line no. pointing to free in calcMedian */ by 0x4416C9: main /* Line no pointing to first call of calcMedian */ </code></pre> <p>Is this a problem with <code>free</code>? Am I <code>free</code>ing and <code>calloc</code>ing too frequently? I don't know where to start debugging this. Any help will be wonderful! </p> <p><strong>DISCLAIMER:</strong> The computer with the actual code cannot access the internet. I've reproduced here as accurately as I can the code that causes the problem. If there are any missing semicolons etc. that's my fault, it definitely isn't there in the original code.</p> <p>Edit: Fixed some transcription errors. I'll try and get the original code up ASAP, but from me looking through both of them now this seems to be fine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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