Note that there are some explanatory texts on larger screens.

plurals
  1. POPointers and Dynamic Memory
    primarykey
    data
    text
    <p>I have a function that returns a pointer to an array. I'm running it in a loop and <code>free()</code> seems to be giving me problems. I'm not sure where, but it appears that somewhere in the main loop the memory that I'm trying to free is being used. I'm using Xcode 3.2.1 in 10.6 | Debug | x86_64 build.</p> <p>The program will run through the main loop one time; the second time it encounters the <code>free()</code> it gives me the following error:</p> <pre><code>malloc: *** error for object 0x100100180: incorrect checksum for freed object - object was probably modified after being freed. </code></pre> <p>Can someone point out (no pun intended) what I'm doing wrong with pointers here?</p> <p>Here is the program:</p> <pre><code>int main(int argc, char **argv) { int *partition; int lowerLimit; int upperLimit; // snip ... got lowerLimit and upperLimit from console arguments // this is the 'main loop': for (int i = lowerLimit; i &lt;= upperLimit; i += 2) { partition = goldbachPartition(i); printOutput(partition[0], partition[1], i); free(partition); // I get problems on the second iteration here } return 0; } int *goldbachPartition(int x) { int solved = 0; int y, z; int *primes; int *result; result = intAlloc(2); primes = atkinsPrimes(x); for (int i = intCount(primes)-1; i &gt;= 0; i--) { y = primes[i]; for (int j = 0; j &lt; y; j++) { z = primes[j]; if (z + y &gt;= x) { break; } } if (z + y == x) { solved = 1; result[0] = y; result[1] = z; break; } else if (y == z) { result[0] = 0; result[1] = 0; break; } } free(primes); return result; } int *atkinsPrimes(int limit) { int *primes; int *initialPrimes; int *filtered; int *results; int counter = 0; int sqrtLimit; int xLimit; int resultsSize; primes = intAlloc(limit+1); intFillArray(primes, limit+1, 0); sqrtLimit = floor(sqrt(limit)); xLimit = floor(sqrt((limit+1) / 2)); // these loops are part of the Atkins Sieve implementation for (int x = 1; x &lt; xLimit; x++) { int xx = x*x; for (int y = 1; y &lt; sqrtLimit; y++) { int yy = y*y; int n = 3*xx + yy; if (n &lt;= limit &amp;&amp; n % 12 == 7) { primes[n] = (primes[n] == 1) ? 0 : 1; } n += xx; if (n &lt;= limit &amp;&amp; (n % 12 == 1 || n % 12 == 5)) { primes[n] = (primes[n] == 1) ? 0 : 1; } if (x &gt; y) { n -= xx + 2*yy; if (n &lt;= limit &amp;&amp; n % 12 == 11) { primes[n] = (primes[n] == 1) ? 0 : 1; } } } } for (int n = 5; n &lt; limit; n++) { if (primes[n] == 1) { for (int k = n*n; k &lt; limit; k += n*n) { primes[k] = 0; } } } initialPrimes = intAlloc(2); if (limit &gt;= 2) { initialPrimes[counter++] = 2; } if (limit &gt;= 3) { initialPrimes[counter++] = 3; } filtered = intFilterArrayKeys(primes, limit+1); results = intMergeArrays(initialPrimes, filtered, counter, trueCount(primes, limit+1)); resultsSize = counter + trueCount(primes, limit+1); free(primes); free(initialPrimes); free(filtered); results[resultsSize] = 0; return results; } int trueCount(int *subject, int arraySize) { int count = 0; for (int i = 0; i &lt; arraySize; i++) { if (subject[i] == 1) { count++; } } return count; } int intCount(int *subject) { // warning: expects 0 terminated array. int count = 0; while (*subject++ != 0) { count++; } return count; } void intFillArray(int *subject, int arraySize, int value) { for (int i = 0; i &lt; arraySize; i++) { subject[i] = value; } } int *intFilterArrayKeys(int *subject, int arraySize) { int *filtered; int count = 0; filtered = intAlloc(trueCount(subject, arraySize)); for (int i = 0; i &lt; arraySize; i++) { if (subject[i] == 1) { filtered[count++] = i; } } return filtered; } int *intMergeArrays(int *subject1, int *subject2, int arraySize1, int arraySize2) { int *merge; int count = 0; merge = intAlloc(arraySize1 + arraySize2); for (int i = 0; i &lt; arraySize1; i++) { merge[count++] = subject1[i]; } for (int i = 0; i &lt; arraySize2; i++) { merge[count++] = subject2[i]; } return merge; } int *intAlloc(int amount) { int *ptr; ptr = (int *)malloc(amount * sizeof(int)); if (ptr == NULL) { printf("Error: NULL pointer\n"); } return ptr; } void printOutput(int num1, int num2, int rep) { if (num1 == 0) { printf("%d: No solution\n", rep); exit(0); } else { printf("%d = %d + %d\n", rep, num1, num2); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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