Hello, I am working on a merge sort but I keep getting the following error : ds07(6443) malloc: *** error for object 0x100100098: incorrect checksum for freed object - object was probably modified after being freed. I don't see what my error is with the creation and deletion of my dynamic memory. Any help would be appreciated
void Array::mergeSort (void)
{
mergeSort (0, size-1); // call to private : merge
}
void Array::mergeSort (int first, int last)
{
if (last > first)// more than one in subarray
{
int mid = (first + last) / 2;
mergeSort (first, mid);
mergeSort (mid+1, last);
merge (first, last);
}
}
void Array::merge (int first, int last)
{
int* merged = new int [last-first+1];
int mid = (first + last) / 2;
int secList = mid + 1; // beginning of second list
int firstList = first; //beginning of first list
int j = first; // used for array index assignment
while ( (firstList <= mid) && (secList <= last) )// both lists #1 & #2 are not exhausted
{
if (data[firstList] < data[secList])// data in list #1 < data in list #2
{
merged[j] = data[firstList];// <- data in list #1
firstList++;
}
else
{
merged[j] = data[secList]; // <- data in list #2
secList++;
}
j++;
} // end while
if (firstList > mid)
{
for (int q = secList; q <= last; q++){
merged[j] = data[q];
j++;
}
}
else
{
for (int r = firstList; r <= mid; r++)
{
merged[j] = data[r];
j++;
}
}
for (int i = first; i <= last; i++)
{
data[i] = merged[i];// overwrite original positions with "merged"
}
delete [ ] merged; //commented out due to segmentation fault
merged = NULL;
}