title: What happens if you don't free dynamically allocated memory in a function?
body: <p>I am learning how to write functions in C to accept an array and return a modified array.</p>
  
  <p>In function <code>testfunc</code> (which is supposed to simply add <code>10</code> to each element of input array <code>b</code>) I am allocating memory for <code>npts</code> number of integers using <code>malloc</code>. But since I want to return this array using a pointer, I am not freeing this memory at the end of the function. Suppose I call this function <code>100</code> times like I am doing in the code, so what happens to the all the memory allocated during the code ? Is the amount of memory used by the code <code>100*10*4</code> bytes ? For a function which doesn't work on dynamic memory allocation, I think memory allocated to variables disappears when the function returns the final value and when it is called again, it again allocates memory and so on. But I am confused as to what happens in this case.</p>
  
  <p>i cannot free the allocated memory inside the function since i need it to return the array to the main function, but also i need to call this function more than 100 times for different arrays, so if it keeps on allocating again and again, it will run out of memory </p>
  
  <p>And is there a way to check how much memory a code is using ? (other than looking at Activity Monitor on Mac-OSX).</p>
  
  <p>Thanks !</p>
  
  <pre><code>/* code to test returning array from functions */
  
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  int* testfunc(int *a,int npts);
  
  int main(int argc, char* argv[])
  {
      int *b;
      int a[10],i,j,npts=10;
  
      b=(int *) malloc(sizeof(int)*npts);
  
      for (j=0; j &lt;100; j++)
      {
          printf("iteration number %d \n",j);
          for (i=0; i&lt;npts; i++)
          {
              a[i]=i;
              printf("%d \n",a[i]);
          }
  
          b=testfunc(a,npts);
  
          printf("returned array \n");
          for (i=0; i&lt;npts; i++)
          {
              printf("%d \n",b[i]);
          }
      }
      printf("the size of one integer is %d \n",sizeof(int));
  
      return 0;
  }
  int* testfunc(int *b,int npts)
  {
      int *c;
      int i=0;
  
      c=(int *) malloc(sizeof(int)*npts);
  
      for (i=0; i&lt;npts; i++)
      {
          c[i]=b[i]+10;
      }
  
      return c;
  }
  </code></pre>
  
  <hr>
  
  <p>This is the possible solution to avoid allocating memory inside a function and being able to call the function multiple times</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;math.h&gt;
  
  void testfunc(int *c,int *d,int npts);
  
  int main(int argc, char* argv[])
  {
      int *a,*b;
      int i,j,npts=10;
  
      a=malloc(sizeof(int)*npts);
      b=malloc(sizeof(int)*npts);
  
      for (j=0; j &lt;100; j++)
      {
          printf("iteration number %d \n",j);
          for (i=0; i&lt;npts; i++)
          {
              a[i]=i;
              printf("%d \n",a[i]);
          }
  
          testfunc(a,b,npts);
  
          printf("returned array \n");
          for (i=0; i&lt;npts; i++)
          {
              printf("%d \n",b[i]);
          }
      }
      printf("the size of one integer is %d \n",sizeof(int));
  
      free(a);
      free(b);
  
      return 0;
  }
  void testfunc(int *c,int *d,int npts)
  {
      int i=0;
  
      for (i=0; i&lt;npts; i++)
      {
          d[i]=c[i]+10;
      }
  }
  </code></pre>