Segmentation Fault Error

I'm working on a merge sort program, and am getting a force close alongside a segmentation fault error. (I'm working in Dev-C++ by Bloodshed). I know these are solved by dereferenced pointers or dangling ones... But I've looked through my code a billion times and can't seem to find the problem. (Probably due to working on it for hours at a time now.)

Is there anyone that woulnd't mind thumbing through my code to see where I messed up and possibly how to fix it? (If that's not too much).

What's odd is that it used to work until I restarted my PC. O.o

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
void mergeSortMerge(double*,int,int,int);
void mergeSort(double *v, int p, int r) //standard mergesort algorithm
  {
      if (p < r)
      {
          int q;
  
          q = (p + r) / 2;
          mergeSort(v, p, q);
          mergeSort(v, q+1, r);
          mergeSortMerge(v, p, q, r);
      }
  
      return;
  }
  
  void mergeSortMerge(double *v, int p, int q, int r) //Merge part of merge algorithm
  {
      int i, j, k,;
      double *B;
  
      B = (double *)malloc(sizeof(int)*(r-p));
     i = p;
     j = q + 1;
     k = 0;
 
     while (i <= q && j <= r)
     {
         if (v[i] < v[j])
         {
             B[k] = v[i];
             i++;
         }
         else
          {
             B[k] = v[j];
            j++;
        }
          k++;
      }
  
     while (i <= q)
     {
          B[k] = v[i];
          i++;
          k++;
      }
      while (j <= r)
     {
          B[k] = v[j];
          j++;
          k++;
      }
  
      k = 0;
      for (i=p; i<=r; i++, k++)
          v[i] = B[k];
  }


And the function call is:
mergeSort( array, 0 , arraySize);

Array being an array of doubles, and arraySize being a predefined int.

I have a feeling it's in the function call...
is r greater than p?

Also are you sure K is not exceeding the length of the array?
Topic archived. No new replies allowed.