title: malloc.c:2379: sysmalloc: Assertion... failed
body: <p>Im currently writing a program that uses Heap class, to find the max value in part of array (size of part is k), which is moving in array, until reaches the end. It crushes on dataset:
  3
  1 2 3
  1
  with misstake: malloc.c:2379: 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.
  It crushes when it tryes to cout the temp.value variable. If i cooment it, everything  works just fine</p>
  <p>Here is my code:</p>
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;cassert&gt;
  
  #define DEFAULT_GROW 2
  
  template&lt;class T&gt;
  class IsLess {
  public:
      bool operator()(const T&amp; lhs, const T&amp; rhs) const {
          return lhs &lt; rhs;
      }
  };
  
  template&lt;class T&gt;
  struct Node {
      T value;
      size_t index;
  };
  
  
  template &lt;class T, class H = IsLess&lt;T&gt;&gt;
  class Heap {
  public:
      Heap();
      Heap(const T* arr, size_t dataSize, H func = IsLess&lt;T&gt;());
      Heap(const Heap&amp;) = delete;
      Heap(Heap&amp;&amp;) = delete;
      Heap operator = (const Heap&amp;) = delete;
      Heap operator = (Heap&amp;&amp;) = delete;
      ~Heap();
      bool IsEmpty();
      void Insert(const T &amp;k);
      Node&lt;T&gt; ExtractMax();
      Node&lt;T&gt; ExtractMaxByIndex(const size_t &amp;i, const size_t &amp;k);
      void InsertNode(const Node&lt;T&gt; &amp;);
  
  private:
      void Grow();
      void BuildHeap();
      void SiftDown(const int &amp;i);
      void SiftUp(int i);
      size_t capacity;
      size_t size;
      Node&lt;T&gt;* data;
      H comparator;
  };
  
  template&lt;class T, class H&gt;
  Heap&lt;T, H&gt;::Heap():
  capacity(0),
  size(0),
  data(nullptr),
  comparator(IsLess&lt;T&gt;())
  {}
  
  template&lt;class T, class H&gt;
  Heap&lt;T, H&gt;::Heap(const T* arr, size_t dataSize, H func) {
      assert(arr != nullptr);
      assert(dataSize &gt; 0);
      data = new Node&lt;T&gt;[dataSize];
      capacity = dataSize;
      size = dataSize;
      comparator = func;
      // std::memcpy(data-&gt;value, arr, dataSize * sizeof(T) - 1);
      for (size_t i = 0; i &lt; dataSize; ++i) {
          data[i].value = arr[i];
          data[i].index = i;
      }
      BuildHeap();
  }
  
  template&lt;class T, class H&gt;
  Heap&lt;T, H&gt;::~Heap() {
      delete[] data;
  }
  
  template&lt;class T, class H&gt;
  void Heap&lt;T, H&gt;::Grow() {
      if (capacity == 0) {
          data = new Node&lt;T&gt;[++capacity];
          return;
      }
      if (size &lt; capacity) {
          return;
      }
      capacity *= DEFAULT_GROW;
      Node&lt;T&gt; *buf = new Node&lt;T&gt;[capacity];
      std::memcpy(buf, data, sizeof(Node&lt;T&gt;) * size - 1);
      delete[] data;
      data = buf;
  }
  
  template&lt;class T, class H&gt;
  void Heap&lt;T, H&gt;::SiftDown(const int &amp;i) {
      size_t leftChild = i * 2 + 1;
      size_t rightChild = i * 2 + 2;
      size_t largest = i;
      if(leftChild &lt; size
      &amp;&amp; comparator(data[largest].value, data[leftChild].value)) {
          largest = leftChild;
      }
      if(rightChild &lt; size
      &amp;&amp; comparator(data[largest].value, data[rightChild].value)) {
          largest = rightChild;
      }
      if(largest != i) {
          std::swap(data[i], data[largest]);
          SiftDown(largest);
      }
  }
  
  template&lt;class T, class H&gt;
  void Heap&lt;T, H&gt;::SiftUp(int i) {
      while(i &gt; 0) {
          int parent = (i - 1) / 2;
          if(comparator(data[i].value, data[parent].value)) {
              return;
          }
          std::swap(data[i], data[parent]);
          i = parent;
      }
  }
  
  template&lt;class T, class H&gt;
  void Heap&lt;T, H&gt;::BuildHeap() {
      for (int i = size / 2 - 1 ; i &gt;= 0 ; --i) {
          SiftDown(i);
      }
  }
  
  template&lt;class T, class H&gt;
  bool Heap&lt;T, H&gt;::IsEmpty() {
      return size == 0;
  }
  
  template&lt;class T, class H&gt;
  void Heap&lt;T, H&gt;::Insert(const T &amp;k) {
      if (IsEmpty() || size == capacity) {
          Grow();
      }
      data[size].value = k;
      data[size].index = size;
      SiftUp(size++);
  }
  
  template&lt;class T, class H&gt;
  Node&lt;T&gt; Heap&lt;T, H&gt;::ExtractMax() {
      assert(!IsEmpty());
      Node&lt;T&gt; result = data[0];
      data[0] = data[--size];
      if (!IsEmpty()) {
          SiftDown(0);
      }
      return result;
  }
  
  template&lt;class T, class H&gt;
  void Heap&lt;T, H&gt;::InsertNode(const Node&lt;T&gt; &amp;node) {
      if (IsEmpty() || size == capacity) {
          Grow();
      }
      data[size] = node;
      SiftUp(size++);
  }
  
  template&lt;class T, class H&gt;
  Node&lt;T&gt; Heap&lt;T, H&gt;::ExtractMaxByIndex(const size_t &amp;i, const size_t &amp;k) {
      assert(!IsEmpty());
      Node&lt;T&gt; result = {0, i + k + 1}; //  чтобы условие не выполнилось с 1 раза
      Node&lt;T&gt; *extracted = new Node&lt;T&gt;[k];
      size_t extractedIndex = 0;
      while(result.index &lt; i || result.index &gt; i + k - 1) {
          result = ExtractMax();
          extracted[extractedIndex++] = result;
      }
      for (size_t j = 0 ; j &lt; extractedIndex ; ++j) {
          InsertNode(extracted[j]);
      }
      delete [] extracted;
      return result;
  }
  
  void Answer (unsigned int *arr, const size_t &amp;n, const size_t &amp;i, const size_t &amp;k) {
      assert(arr != nullptr);
      Heap&lt;unsigned&gt; heap(arr, n);
      //size_t resSize = n - k + 1;
  
      Node&lt;unsigned int&gt; temp = heap.ExtractMaxByIndex(i, k);
      std::cout &lt;&lt; temp.value &lt;&lt; &quot; &quot;;  // This line causes the misstake
  }
  
  int main() {
      size_t n = 0;
      std::cin &gt;&gt; n;
      unsigned int *arr = new unsigned int[n];
      for (size_t i = 0; i &lt; n; ++i) {
          std::cin &gt;&gt; arr[i];
      }
      size_t k = 0;
      std::cin &gt;&gt; k;
      for (size_t i = 0 ; i &lt; 1 ; ++i) {
          Answer(arr, n, i, k);
      }
      delete[] arr;
      return 0;
  }
  
  Input: 
  3
  1 2 3
  1
  
  Output:
  1 2 3
  </code></pre>