Runtime Error Help

So, my code is running without any compiler errors. However, when it outputs the results to the screen, I get the error _ctrisvalidheappointer(block). I know that it's an assert error, however I've tried many times (even asked my teacher, who didn't know exactly what the issue was) to fix it, but to no avail. Now, while I know it's an assertion error, I don't really know what assertion means, however with the inclusion of cassert's library, I can't help but feel that there shouldn't be an error.

Could someone please help another friendly coder out. :)

Now, I believe the error could be in this block:
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
template<class Type>
void linkedListType<Type>::divideAt(linkedListType<Type> &secondList, const Type& item)
{
	nodeType<Type> *current; 
                                        //Adds current so the list can be traversed, and when item
					//is found, makes current become equal to the node that item
					//was found at, thus becoming the first node in the second
					//list.

	nodeType<Type> *trail;
                 //Trails current to be able to adjust and become the new
		 //last node of the first list.

	trail = new nodeType<Type>; 
                 //Creates an extra node, so that current can be properly
		 //followed.

	current = first->link; 
                  //Sets the current node equal to the first node of the first list

	trail->link = current; 
                  //Trail now points to the node current is at

	if (current->info != item && current->link != nullptr) 
                  //if item isn't found where current's
	          //pointing to, and current isn't
	{
		current = current->link; 
                   //current points to the next node in the list
		
                trail = trail->link; 
                   //current points to the next node in the list
	} //End if
	
		last = trail;  
                    //Sets the new last as being equal to trail.

		trail->link = nullptr; 
                    //Sets the link that trail is pointing to to be
		    //null.
	
	while (current->info == item && current->link != nullptr)
	{
		secondList.first = current; 
                     //Sets the first node of the second list to be equal to
		     //current, which contains the item being searched for.

		current = current->link; 
                     //current points to the next node in the list.
	} //End while

	secondList.last = last; 
                     //Sets the last node of the second list as the last node of the
		     //first list.
}


But, it could be in this block, too:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class Type>
void unorderedLinkedList<Type>::mergeLists(unorderedLinkedList<Type> &list1,
	unorderedLinkedList<Type> &list2)
{
	list1.last->link = list2.first; 
                 //Sets the last node of the first list's link
                 //to the first node of the second list, rather
		 //than nullptr.

	first = list1.first; 
                 //The first node of the new list will be the first node of
		 //the first list.

	last = list2.last; 
                  //The last node of the second list will be the last node of
		  //the second list.
}


(Also, I realize I could possibly have my understanding of compile and runtime backwards.)
Last edited on
For reference:

template <class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};

first is basically equal to the first node of a list.
last is basically equal to the last node of a list.
item is a number, inputted by the user, which will be searched for in the list against the values of info.
Last edited on
UPDATE:

I believe that my issue would be that I need to de-allocate the memory of the first list, the memory from the nodes after line 58: trail->link = nullptr; executes. However, I would think that setting trail->link to be equal to nullptr would delete any nodes after it. Could someone please confirm if this is the issue?

NEW UPDATE:

I have now found out that the debug error is, in fact, with my mergeLists function and has nothing to do with my divideAt function (after commenting out the mergeLists function). I added last->link = nullptr; on the end, but unfortunately that didn't work, either. Any input?
Last edited on
Topic archived. No new replies allowed.