title: "Dynamic (pointer) arrays" across functions
body: <p>I wish to create a function which returns all the possible numbers a number can be divided by. Also I wish to use these returned results in another function which looks for all the prime numbers in those results and in turn returns those.</p>
  
  <p>Long story short, I want to be able to return "dynamic arrays" and use them again in another function.</p>
  
  <p>What I have so far:</p>
  
  <pre><code>int *division (long a) {
      int i;
      int *divResult = malloc(a * sizeof(long)); //Ultimately I want to allocate space dynamically instead of reserving too much space
  
      if (naturalNumber(a)) { //What it basically does is "fmod(a, 1)" (returns boolean)
      divLength = 0; //A global int set to trace the length of division()
          for (i = 0; i &lt; a + 1; i++) {
              if (divisor(i, a)) { //Checks wether the division of a by i returns a whole number (returns boolean)
                  //divResult = realloc(divResult, sizeof(long)); //This gives seg faults and errors durning runtime; To be clear I am not exactly using it like this. What I do is I adjust the "malloc(a*sizeof(long))" to "malloc(sizeof(long))"
                  divResult[i] = i; //If i is a divisor add it to the pointer
                  divLength += 1; //track how long the current division is
              }
          }
      }
  
      free(divResult); //I was hoping this to remove the divResult from the memory, sadly it is still there and doesnt give me an error of me trying to return a NULL value
      //printf("Mem: %p\n", (void *)divResult);
      return divResult; //Return a "dynamic array" so that another function can use it
  }
  </code></pre>
  
  <p>As you can see, I am aware of the fact that i have to use pointers instead of real arrays because those cannot be returned.
  I know this works and has returned true values.</p>
  
  <p>Also I want this function to use the division() function to see the divisors of a number and put them in an array:</p>
  
  <pre><code>int *primes (long a) { //I'm trying to reuse the same tactic as before:
      int i;
      int *primeResult = malloc(a * sizeof(long)); //Allocating it again...
  
      if (naturalNumber(a)) { //See previous codeblock
          for (i = 0; i &lt; a + 1; i++) {
              division(i); //Calling the function here to divide each number from 1 to "a" so I am able to see which one is of size 2 (in other words the division of number *1 to a* and check wether it is a prime number (it has in total only 2 divisors))
              printf("len = %d\n", divLength); //prints the current length of division()
              if (divLength == 2) { //If the division() "length" is 2
                  primeResult[i] = i; //add i to primes
              }
          }
      }
  
      free(primeResult); //Same as above
      return primeResult;
  }
  </code></pre>
  
  <p>As you might guess I'm not getting the expected results.</p>
  
  <p>Using:</p>
  
  <pre><code>int *div;
  div = division(a) + 1;
  for (i = 0; i &lt; a; i++) {
      if (*(div + i) != 0)
          printf("*(div + %d) : %d\n", i, *(div + i));
  }
  
  int *prime;
  prime = primes(a) + 1;
  for (i = 0; i &lt; a; i++) {
      if (*(prime + i) != 0)
          printf("*(prime + %d) : %d\n", i, *(prime + i));
  }
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>len = 6
  len = 1
  len = 2
  len = 2
  len = 3
  len = 2
  len = 4
  len = 2
  len = 4
  len = 3
  len = 4
  len = 2
  len = 6
  *(prime + 1) : 2
  *(prime + 2) : 3
  *(prime + 3) : 4
  *(prime + 4) : 5
  *(prime + 5) : 6
  *(prime + 6) : 7
  *(prime + 10) : 11
  *(prime + 11) : 12
  </code></pre>
  
  <p>I can assure you that if I comment out all the traces except the ones for division() and call primes() I still get as a result:</p>
  
  <pre><code>*(div + 1) : 2
  *(div + 2) : 3
  *(div + 3) : 4
  *(div + 5) : 6
  *(div + 11) : 12
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  len = 0
  *(prime + 1) : 2
  *(prime + 2) : 3
  *(prime + 3) : 4
  *(prime + 5) : 6
  *(prime + 11) : 12
  </code></pre>
  
  <p>So, I know my code is bad; that's why I'm asking now (I'm still learning), I ask you:</p>
  
  <ul>
  <li>Why is it still returning the values of division() even though I have "freed" the memory of divResult?</li>
  <li>How can I get realloc() to work?</li>
  <li>Is there any easier way to do all this in <strong>separate</strong> functions?</li>
  </ul>
  
  <p>And last but not least: did anyone actually understand what I'm asking?</p>