std::bad_alloc for merge sort

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!

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
  #include <iostream>
#include <string>
#include <iomanip>
#include <cctype>
#include<vector>
using namespace std;
template <class T>
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 <class T>
void Sorting<T>::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 <class T>
void Sorting<T>::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 <class T>
void Sorting<T>::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 <typename T>
void Sorting<T>::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 <typename T>
void Sorting<T>::Merge(T arr[], int start, int end)
{
    int z, x, y, mid;
    vector<T> 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 <typename T>
void Sorting<T>::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<Student> 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:
1
2
3
4
    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.