title: Malloc is throwing an error for pointer being freed
body: <p>so I have a function that creates a flexible array type and all paths use calloc to define the size of the array. However when I try to destroy the struct, it says that the memory was not allocated.</p>
  <pre><code>typedef struct {
      int size;
      void *items;
  } Array;
  
  typedef enum {
      CHAR,
      INT,
      FLOAT,
      DOUBLE
  } datatype;
  
  Array *create_array(int initial_size, datatype type)
  {
      Array *arr = malloc(sizeof(Array));
  
      arr-&gt;size = initial_size;
  
      switch (type) {
          case CHAR:
              arr-&gt;items = (char *) calloc(5, sizeof(char));
              break;
          case INT:
              arr-&gt;items = (int *) calloc(arr-&gt;size, sizeof(int));
              break;
          case FLOAT:
              arr-&gt;items = (float *) calloc(arr-&gt;size, sizeof(float));
              break;
          case DOUBLE:
              arr-&gt;items = (double *) calloc(arr-&gt;size, sizeof(double));
              break;
          default:
              exit(1); // will handle later on
              break;
      }
      return arr;
  }
  
  void destroy_array(Array *arr)
  {
      free(arr-&gt;items); // this is the line that throws the error
      free(arr);
  }
  
  </code></pre>
  <p>The exact error is:</p>
  <p><em>flexibleArray(63110,0x10c3c6600) malloc: *** error for object 0x600000c21120: pointer being freed was not allocated
  flexibleArray(63110,0x10c3c6600) malloc: *** set a breakpoint in malloc_error_break to debug</em></p>
  <p>I just don't understand why memory I'm allocating with calloc does not require to be freed.</p>
  <p>Thanks for anyone's input!</p>
  <p><strong>Main Loop</strong></p>
  <p>There is just a lot of random code in there that I am using to test out changing the size of the array. I don't think it would affect the outcome of the destroy function.</p>
  <pre><code>    int main() {
          Array *newArr = create_array(5, INT);
          int *val;
          int w = 20;
          val = &amp;w;
  
          printf(&quot;%s\n&quot;, typename(w));
  
          ((int *)newArr-&gt;items)[0] = *val;
          ((int *)newArr-&gt;items)[1] = 35;
          ((int *)newArr-&gt;items)[2] = 20;
          ((int *)newArr-&gt;items)[3] = 351;
          ((int *)newArr-&gt;items)[4] = 315;
  
          for(int i = 0; i &lt; newArr-&gt;size; i++) {
              printf(&quot;%d\n&quot;, ((int *)newArr-&gt;items)[i]);
          }
  
          realloc(newArr-&gt;items, 10);
  
          ((int *)newArr-&gt;items)[5] = 50;
          for(int i = 0; i &lt; newArr-&gt;size; i++) {
              printf(&quot;%d\n&quot;, ((int *)newArr-&gt;items)[i]);
          }
  
          destroy_array(newArr);
      }
  </code></pre>