title: malloc.c:2451: sYSMALLOc Assertion failed
body: <p>I keep getting this error:</p>
  
  <blockquote>
    <p>malloc.c:2451: sYSMALLOc: Assertion (old_top == (((mbinptr) (((char *) &amp;((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) &amp; ~((2 * (sizeof(size_t))) - 1))) &amp;&amp; ((old_top)->size &amp; 0x1) &amp;&amp; ((unsigned long)old_end &amp; pagemask) == 0)' failed.</p>
  </blockquote>
  
  <p>This is the piece of code that causes the error:</p>
  
  <pre><code>BinaryTree * oldTree, * temp;
  BinaryTree ** sorted;
  int i = 0, j = 0, k = 0;
  
  oldTree = (BinaryTree*)malloc(cnt * sizeof(BinaryTree));
  sorted = (BinaryTree**)malloc(cnt * sizeof(BinaryTree*));
  
  /*
  for (i = 0; i &lt; cnt; i++)
  {
      oldTree[i].value = table[i].data;
      oldTree[i].freq = table[i].cnt;
      oldTree[i].left = NULL;
      oldTree[i].right = NULL;
      oldTree[i].parent = NULL;
      sorted[i] = &amp;oldTree[i];
  }
  
  while (cnt &gt;= 2)
  {
      temp = (BinaryTree*)malloc(sizeof(BinaryTree));
  
      temp-&gt;value = -1;
      temp-&gt;freq = sorted[0]-&gt;freq + sorted[1]-&gt;freq;
      temp-&gt;left = sorted[0];
      temp-&gt;right = sorted[1];
      temp-&gt;parent = NULL;
      sorted[0]-&gt;parent = temp;
      sorted[1]-&gt;parent = temp;
  
      for (j = 0; j &lt; cnt - 2; j++)
          sorted[j] = sorted[j + 2];
  
      j = 0;
      while (temp-&gt;freq &gt;= sorted[j]-&gt;freq &amp;&amp; j &lt; cnt - 2)
          j++;
  
      for (i = cnt - 1; i &gt; j; i--)
          sorted[i] = sorted[i - 1];
  
      sorted[j] = temp;
  
      cnt--;
      k++;
  }
  createPattern(oldTree, table, cnt); 
  
  free(oldTree); // May be wrong
  free(temp);
  free(sorted);
  */
  </code></pre>
  
  <p>Even comment out every bit of this code with the exception of the malloc lines themselves leads to this error. </p>
  
  <p>Any ideas?</p>