### Forum

For merge sorts I've been dealing with this error for quite sometime and cannot figure out how to solve the memory leak issue. Solutions would be great!

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211`` `````` #include #include #include #include #include using namespace std; template class Sorting { public: void print(T a[], int); void selection_sort(T a[], int); void InsertionSort(T arr[], int); void BubbleSort(T arr[], int); void Merge(T arr[], int, int); void MergeSort(T arr[], int , int); }; template void Sorting::print(T a[], int s) { cout << "Student ID:\tStudent name:" << endl; for (int i = 0; i < s; i++) { cout << a[i].get_id() << "\t\t\t" << a[i].get_name(); cout << endl; } } template void Sorting::selection_sort(T a[], int s) { int min_index; for (int i = 0; i < s - 1; i++) { min_index = i; for (int j = i + 1; j < s; j++) if (a[j] < a[min_index]) min_index = j; if (min_index != i) swap(a[min_index], a[i]); } } template void Sorting::InsertionSort(T arr[], int n) { int i, j; T temp; for (int i = 1; i < n; ++i) { temp = arr[i]; j = i - 1; while (j >= 0 && temp < arr[j]) { arr[j + 1] = arr[j]; j = j - 1; } arr[j + 1] = temp; } } template void Sorting::BubbleSort(T arr[], int n) { for (int i = 0; i < n - 1; ++i) { for (int j = 0; j < n - i - 1; ++j) { if (arr[j + 1] < arr[j]) { T temp = arr[j + 1]; arr[j + 1] = arr[j]; arr[j] = temp; } } } } template void Sorting::Merge(T arr[], int start, int end) { int z, x, y, mid; vector temp(end - start + 1); mid = (start + end) / 2; z = 0; x = start; y = mid + 1; while (x <= mid && y <= end) { if (arr[x] < arr[y]) { temp[z] = arr[x]; ++x, ++z; } else { temp[z] = arr[y]; ++y, ++z; } } while (x <= mid) { temp[z] = arr[x]; ++x, ++z; } while (y <= end) { temp[z] = arr[y]; ++y, ++z; } // write the merged sequence back to the original array for (int i = start; i <= end; ++i) { arr[i] = temp[i - start]; } } // template function to perform merge sort on array, arr template void Sorting::MergeSort(T arr[], int start, int end) { if (start < end) { int mid = (start + end) / 2; MergeSort(arr, start, mid); // merge sort the elements in range [start, mid] MergeSort(arr, mid + 1, end); // merge sort the elements in range [mid+1, end] Merge(arr, start, end); // merge the above 2 componenets } } class Student { private: int ID; string Name; public: Student(int, string); Student(); void set_id(int); int get_id(); void set_name(string); string get_name(); bool operator<(Student &); }; Student::Student(int i, string n) { ID = i; Name = n; } Student::Student() { ID = 0; Name = " "; } void Student::set_id(int i) { ID = i; } int Student::get_id() { return ID; } void Student::set_name(string n) { Name = n; } string Student::get_name() { return Name; } bool Student::operator<(Student &obj) { return (this->ID < obj.ID); } int main() { Student a[5]; a[0].set_id(40); a[0].set_name("john"); a[1].set_id(55); a[1].set_name("jake"); a[2].set_id(99); a[2].set_name("ron"); a[3].set_id(35); a[3].set_name("sam"); a[4].set_id(70); a[4].set_name("james"); Sorting obj; //obj.print(a, 5); //obj.selection_sort(a, 5); //obj.print(a, 5); //obj.InsertionSort(a, 5); //obj.print(a, 5); //obj.BubbleSort(a, 5); obj.print(a, 5); obj.MergeSort(a, 0, 5); obj.print(a, 5); return 0; }``````
Last edited on
The problem is that the index is out of bounds at the end. The last possible index is (line 198) 4. So this:
 ``1234`` `````` for (int i = start; i <= end; ++i) { arr[i] = temp[i - start]; }``````
will lead to undefined behavior most likely a crash. So either `i < end` or you pass 4: `obj.MergeSort(a, 0, 4)`

It is not a memory leak.

By the way: A class without member variable does not make too much sense...
 I've been dealing with this error for quite sometime and cannot figure out how to solve

What debugging of the code have you done? Have you used the debugger to trace through the code step by step looking at the contents of variables after each step? Also try working through the code using pen/paper just like the computer would. This can often show issues with 'edge' conditions etc.

Learning how to debug a compilable program is an art that needs to be learnt. Even very experienced programs can have issues in their code the first time (myself included!). Programmers have to be able to track these down, find the issue and fix it.

What compiler are you using?
Topic archived. No new replies allowed.