title: malloc: Incorrect checksum for freed object
body: <p>Below is function to do merge sort. but I meet error when I execute. The allocated memory(aux) has been freed every time in function <code>merge</code>, why it will be modified after freed?</p>
  <blockquote>
  <pre><code>a.out(65287,0x1112bedc0) malloc: Incorrect checksum for freed object
  0x7ff9a4c05888: probably modified after being freed. Corrupt value:
  0xb00000003 a.out(65287,0x1112bedc0) malloc: *** set a breakpoint in
  malloc_error_break to debug Abort trap: 6
  </code></pre>
  </blockquote>
  <pre><code>void merge(int arr[], int lo, int mid, int hi) {
      int i = lo; 
      int j = mid + 1;
      int *aux = (int *)malloc((hi - lo + 1) * sizeof(int));
      for (int k = lo; k &lt;= hi; k++) {
          aux[k] = arr[k];
      }   
      for (int k = lo; k &lt;= hi; k++) {
          if (i &gt; mid)
              arr[k] = aux[j++];
          else if (j &gt; hi)
              arr[k] = aux[i++];
          else if (aux[i] &gt; aux[j])
              arr[k] = aux[j++];
          else
              arr[k] = aux[i++];
      } 
      free(aux);
  }
  
  void mergesort1(int arr[], int lo, int hi) {
      if (lo &gt;= hi)
          return;
      int mid = lo + (hi - lo) / 2;
      mergesort1(arr, lo, mid);
      mergesort1(arr, mid + 1, hi);
      merge(arr, lo, mid, hi);
  }
  </code></pre>
  <p>call with:</p>
  <pre><code>mergesort1(arr, 0, 9);
  </code></pre>