title: Valgrind gives me Invalid write of size 4 Address 0x4037268 is not stack'd, malloc'd or (recently) free'd
body: <pre><code>void merge(int left, int mid, int right)
  {
    // sublist sizes
    int left_size = mid - left + 1;
    int right_size = right - mid;
  
    // counts
    int i, j, k;
  
    // create left and right arrays
    B left_list = malloc(left_size*sizeof(B));
    B right_list = malloc(right_size*sizeof(B));
  
    for (i = 0; i &lt; left_size; i++)
      left_list[i] = list[left + i];
  
    for (j = 0; j &lt; right_size; j++)
      right_list[j] = list[mid + j + 1];
  
    // reset counts
    i = 0; j = 0;
  
    for (k = left; k &lt;= right; k++)
    {
      if (j == right_size)
        list[k] = left_list[i++];
      else if (i == left_size)
        list[k] = right_list[j++];
      // here we call the given comparision function
      else if (compar(&amp;left_list[i], &amp;right_list[j]) &lt; 0)
        list[k] = left_list[i++];
      else
        list[k] = right_list[j++];
    }
  }
  
  void sort(int left, int right)
  {
    if (left &lt; right)
    {
      // find the pivot point
      int mid = (left + right) / 2;
  
      // recursive step
      sort(left, mid, compar);
      sort(mid + 1, right, compar);
  
      // merge resulting sublists
      merge(left, mid, right, compar);
    }
  }
  </code></pre>
  
  <p>I just tried to test my program with valgrind, and got this below. </p>
  
  <pre><code>==8679== Invalid write of size 4
  ==8679==    at 0x8048A30: merge (program.c:96)
  ==8679==    by 0x8048BF5: sort (program.c:116)
  ==8679==    by 0x8048C21: user_interface (program.c:124)
  ==8679==    by 0x8048E99: main (program.c:175)
  ==8679==  Address 0x4037268 is not stack'd, malloc'd or (recently) free'd
  </code></pre>