title: Mysterious malloc: sysmalloc: Assertion failed error
body: <p>I am getting a mysterious error and I have no idea why.  This code runs several times before failing, and it always fails at the same point.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>    assert(size &gt; 0);
      int* sorted = malloc(size * sizeof(int));
  </code></pre>
  
  <p>And here is the error I am getting when I run it:</p>
  
  <pre><code>    malloc.c:2369: sysmalloc: Assertion `(old_top == (((mbinptr) (((char *) &amp;((av)-&gt;bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) &amp;&amp; old_size == 0) || ((unsigned long) (old_size) &gt;= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) &amp; ~((2 * (sizeof(size_t))) - 1))) &amp;&amp; ((old_top)-&gt;size &amp; 0x1) &amp;&amp; ((unsigned long)old_end &amp; pagemask) == 0)' failed.
  </code></pre>
  
  <p>I have tried printing out size and it is 1 in this case.  This code runs several times with varying values of size, including 1, before failing.</p>
  
  <p>Does anyone have any idea what I am doing wrong?</p>
  
  <p>Thanks.</p>