malloc: error for object: pointer being freed not allocated

I've created a member function that can merge two sorted linked lists. I'm fairly confident that it does the job, I used a while loop inside the function to display the merged list contents and it gave back the correct values in order. However, I've always gotten the below error whenever I call the function and for the life of me can't figure out why. Any insight would be greatly appreciated, thank you



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
    //Member function: Merge the two lists into one list.
    void merge(List l1, List l2) {
        //Create dummy node
        Node* l3;
        l3 = new Node;
        l3->data = -1;

        //Assign pointer to dummy node
        Node* last = l3;
        Node* h1 = l1.head;
        Node* h2 = l2.head;

        while ((h1 != nullptr) && (h2 != nullptr)) {
            if (h1->data < h2->data) {
                last->next = h1;
                last = h1;
                h1 = h1->next;
            }
            else {
                last->next = h2;
                last = h2;
                h2 = h2->next;
            }
            //Make sure no two numbers are the same before being added.
            if (h1->data == h2->data)
                h1 = h1->next;
        }
        if (h1 != nullptr)
            last->next = h1;
        if (h2 != nullptr)
            last->next = h2;

        l3 = l3->next;
    };



//Main Code.
int main() {
    List l1, l2, l3;
    get_file(l1);
    get_file(l2);
    cout << "There are " << l1.length() << " values in the first list: " << endl;
    l1.list_out();
    cout << endl << "There are " << l2.length() << " values in the second list. " << endl;
    l2.list_out();
    l3.merge(l1, l2);
    cout << endl << "The merged list contains " << l3.length() << " values: " << endl;
    l3.list_out();

    return 0;
}


//Output:
Enter file name: v1.dat
Enter file name: v2.dat
There are 7 values in the first list: 
9 17 21 22 33 35 42 

There are 10 values in the second list. 
17 19 21 22 25 26 33 35 42 56 
a.out(7014,0x10311bd40) malloc: *** error for object 0x14c704230: pointer being freed was not allocated
a.out(7014,0x10311bd40) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./a.out
Last edited on
If it's not possible to copy your code into a text file and compile it you're not going to get particularly effective help.

Post the shortest program you can find that still exhibits the problem.
Last edited on
Okay, now I'm more confused than before lol. I've commented out the entirety of merge() and am still getting the error.

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
    //Merge the two lists into one list.
    void merge(List l1, List l2) {
        //Create dummy node
        // Node* l3;
        // l3 = new Node;
        // l3->data = -1;

        //Assign pointer to dummy node
        // Node* last = l3;
        // Node* h1 = l1.head;
        // Node* h2 = l2.head;

        // while ((h1 != nullptr) && (h2 != nullptr)) {
        //     if (h1->data < h2->data) {
        //         last->next = h1;
        //         last = h1;
        //         h1 = h1->next;
        //     }
        //     else {
        //         last->next = h2;
        //         last = h2;
        //         h2 = h2->next;
        //     }

        //     //Make sure no two numbers are the same before being added.
        //     if (h1->data == h2->data)
        //         h1 = h1->next;

        // }
        // if (h1 != nullptr)
        //     last->next = h1;
        // if (h2 != nullptr)
        //     last->next = h2;

        // l3 = l3->next;
    };


//Output:
There are 7 values in the first list: 
9 17 21 22 33 35 42 

There are 10 values in the second list. 
17 19 21 22 25 26 33 35 42 56 

The merged list contains 0 values: 

a.out(7231,0x104b07d40) malloc: *** error for object 0x12ef040b0: pointer being freed was not allocated
a.out(7231,0x104b07d40) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./a.out
Last edited on
I'm guessing big time that it has something to to do with when I'm calling the function in main. Because the error only pops up once the function is called.

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
function:
    //Merge the two lists into one list.
    void merge(List l1, List l2) {
        //Create dummy node
        // Node* l3;
        // l3 = new Node;
        // l3->data = -1;

        //Assign pointer to dummy node
        // Node* last = l3;
        // Node* h1 = l1.head;
        // Node* h2 = l2.head;

        // while ((h1 != nullptr) && (h2 != nullptr)) {
        //     if (h1->data < h2->data) {
        //         last->next = h1;
        //         last = h1;
        //         h1 = h1->next;
        //     }
        //     else {
        //         last->next = h2;
        //         last = h2;
        //         h2 = h2->next;
        //     }

        //     //Make sure no two numbers are the same before being added.
        //     if (h1->data == h2->data)
        //         h1 = h1->next;

        // }
        // if (h1 != nullptr)
        //     last->next = h1;
        // if (h2 != nullptr)
        //     last->next = h2;

        // l3 = l3->next;
    };

Main:
//Main Code.
int main() {
    List l1, l2, l3;
    get_file(l1);
    get_file(l2);
    cout << "There are " << l1.length() << " values in the first list: " << endl;
    l1.list_out();
    cout << endl << "There are " << l2.length() << " values in the second list. " << endl;
    l2.list_out();
    l3.merge(l1, l2);
    cout << endl << "The merged list contains " << l3.length() << " values: " << endl;
    l3.list_out();

    return 0;
}

Output:
There are 7 values in the first list: 
9 17 21 22 33 35 42 

There are 10 values in the second list. 
17 19 21 22 25 26 33 35 42 56 

The merged list contains 0 values: 

a.out(7231,0x104b07d40) malloc: *** error for object 0x12ef040b0: pointer being freed was not allocated
a.out(7231,0x104b07d40) malloc: *** set a breakpoint in malloc_error_break to debug
zsh: abort      ./a.out

Last edited on
Because you didn't
post the shortest program you can find that still exhibits the problem
I won't bother to analyze your program fragment because the root cause is probably elsewhere.

The error message is a symptom of wrong memory management in the List class.
It's likely that the symptoms occur when the formal parameters to List::merge are destroyed, and that the cause is elsewhere.
Last edited on
You haven't provided the declaration for your List class.

Line 2: One obvious problem is L1 and L2 are passed by value. Did you provide a correct copy constructor. Don't know without seeing the declaration for list. If not, the compiler provides a default copy constructor which is a shallow copy. Assuming List has a memory pointer to the next item, you now have copies of each list which share pointers with the original. When merge exits, l1 and l2 are destructed. I'm assuming your destructor (again not shown) releases the memory.

When main() exits, l1 and l2 (and l3) are destructed. If your destructor is releasing memory, you're releasing memory that has already been released when merge() exited. Please show your List destructor.

Line 2: You should be passing l1 and l2 by const reference. This will avoid the copy.
 
    merge (const List & l1, const List & l2)


Line 49: There's a discrepancy here. You appear to be calling a member function of List. However the merge function at line 2 does not appear to be a member of your class. In either case, you go through all the work to create l3, then l3 is lost when merge() exits.
Last edited on
Topic archived. No new replies allowed.