title: Sysmalloc: Assertion error using vectors
body: <p>While doing this codility <a href="https://app.codility.com/programmers/lessons/6-sorting/max_product_of_three/" rel="nofollow noreferrer">lesson</a> I've encountered some strange behaviour.</p>
  
  <pre><code>int solution(std::vector&lt;int&gt; &amp;A) {
      std::vector&lt;int&gt; B, C;
      B.resize(std::count_if(A.begin(), A.end(), [](int x){return x &lt; 0;}));
      C.resize(A.size() - B.size());
      std::copy(A.begin(), std::remove_copy_if(A.begin(), A.end(), B.begin(), [](int x){return x &gt; 0;}), C.begin());
  
      std::sort(A.begin(), A.end(), [](int x, int y){return x &gt; y;});
      std::sort(B.begin(), B.end(), [](int x, int y){return x &lt; y;});
      std::sort(C.begin(), C.end(), [](int x, int y){return x &gt; y;});
  </code></pre>
  
  <p>This part of function splits a vector into two vectors, 1st with positive and 2nd with negative integers. All vectors are then sorted in a wanted order.</p>
  
  <pre><code>    if (B.size() &gt;= static_cast&lt;size_t&gt;(2)) {
          if (C.size() &gt;= static_cast&lt;size_t&gt;(3)) {
              if (B[0] * B[1] &gt; C[1] * C[2])
                  return B[0] * B[1] * C[0];
          } else if(C.size() == static_cast&lt;size_t&gt;(2)) {
              if (B[0] * B[1] &gt; C[0] * C[1])
                  return B[0] * B[1] * C[0];
          }
      }
      return A[0] * A[1] * A[2];
  }
  </code></pre>
  
  <p>This part finds the biggest multpily product of 3 elements from an input vector.</p>
  
  <p>After reaching return the function crashes and I get an error:</p>
  
  <pre><code>a.out: malloc.c:2394: sysmalloc: Assertion `(old_top == initial_top (av) &amp;&amp;
  old_size == 0) || ((unsigned long) (old_size) &gt;= MINSIZE &amp;&amp; prev_inuse (old_top) &amp;&amp;
  ((unsigned long) old_end &amp; (pagesize - 1)) == 0)' failed.
  Aborted
  </code></pre>
  
  <p>I tried to debug it and I saw that after return statement the debugger jumps to the B &amp; C vectors declaration, and later jumps to the exiting curly brackets.
  I have never seen something, like that and I want to understand it. I'm using g++-7.1 under linux and compile with --std=c++14 flag.
  Thanks in advance.</p>