Error 'st9bad_alloc'

I am writing a Merge Sort Code. But whenever I run the code for a large numbers it gives this error
terminate called after throwing an instance of 'st9bad_alloc'.
The following is the divide portion of my code

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
List<long>* Merge(List<long> *ParentList)
{
        if((ParentList)->Length==1)
        {
            return ParentList;
        }


    List<long> **LeftList=new List<long>*;
    *LeftList=new List<long>;

    List<long> **RightList=new List<long>*;
    *RightList=new List<long>;

    unsigned long long Counter=0;
    unsigned long long MidPoint=ParentList->Length/2;

    ListItem<long> *TempPointer=(ParentList)->head;

        while(Counter!=MidPoint)
        {
            (*LeftList)->insertAtTail(TempPointer->value);
            TempPointer=TempPointer->next;
            Counter++;
        }

        while(TempPointer!=NULL)
        {
            (*RightList)->insertAtTail(TempPointer->value);
            TempPointer=TempPointer->next;
        }

    (*LeftList)=Merge(*LeftList);
    (*RightList)=Merge(*RightList);
    
    return NULL;

}
I am writing a Merge Sort Code. But whenever I run the code for a large numbers it gives this error


Might be because you're leaking memory like a sieve.
I know Thats what the error says, but how do I fix it
Stop using new?
Then I will be declaring on the stack and then it get overloaded
Last edited on
Then, if you must use new, also use delete.

You might also consider cutting down on the 4 news per function call. You don't need that many. And fix your return value. The function returns NULL in every instance.
Topic archived. No new replies allowed.