COMP 53 Debugging (Basic) - sindack/UOP-COMP51-COMP53-Debugging-Help GitHub Wiki

Getting Started

To get started, download the latest version of the repo here: UOP-COMP51-COMP53-Debugging-Help-master.

The project can be found at

/UOP-COMP51-COMP53-Debugging-Help/COMP53/basic/

Open the .vcxproj file to open the project, which is located in Fill in here.

About the Project

Here is the basic flow of the program in main.cpp:

  1. The program will create an integer array filled with random numbers. This is incorrect fix
  2. The integer array will be converted into a linked list of integers mention arrayToLinkedList function.
  3. The result of the linked list and array will be printed out via printArray.
  4. The program will compare the contents of the array and linked list to check if both are equal (compareArrayAndLinkedList).

However, when first running the program, the contents of the array and linked list will not be equal. It is our job to fix the program so that they are the same!

runProgram1 show linked list and array not equal emphasis highlight

Utilizing Breakpoints

To better understand the program, let's add a breakpoint on line 32. Click on the empty space to the left of line number 32.

breakpointLine32

When we run the program (F5), the program will pause at line 32. Visual Studio will also provide us with a generous amount of information about our currently running process. For now, let's take a look at some of the variables in the program. Take a look at the "Autos" section in Visual Studio near the bottom of the window.

variablesMainFunction

If you expand the value of arr by clicking on the right arrow next to the value, you will be able to see each value in the array.

Next, let's observe the value of variable list. Advance the program 1 line by clicking on Step Over or F10 to run the line

add image for step over icon

list = arrayToLinkedList(arr, ARRAY_LENGTH);    // Convert array to linked list.

We can see that list was updated; however, we cannot see all of the values yet. Expand the value of list by clicking on the right arrow next to the value.

expandList1

The sub-variables value and next appear within list. The variable value looks correct; it is equal to the first item of arr. However, we start running into trouble when we expand the value of next.

expandList2

add reminder that 0x00 == NULL

Visual Studio is having trouble displaying the sub-values of next, since it is having trouble reading the values from memory. This actually means that the variable next is equivalent to NULL. But we're expecting it to be the next list item with the value of the 2nd array index! Maybe there's something wrong in the arrayToLinkedList() function...

Digging Deeper

Restart the program, and once we get to the breakpoint, press Step Into (F11) so that we move into the function call of arrayToLinkedList().

stepIntoArrayToLinkedList

Observe the value of list as we continue to step through the program (keep clicking Step Over (F10) until we reach the end of the function). Is anything strange going on?

listObservation

It seems like items are not being added to the end of the list. Let's check the addLinkedListItemToRear() for proper behavior.

Toggle the breakpoint at line 32 to remove it, add a breakpoint at line 71, and restart the program. The program will stop when we begin adding more than 1 item into list.

breakpointLine71

Press Step Into (F11) so that we move into the function call of addLinkedListItemToRear().

stepIntoRear

crop image to only show line 80

Keep clicking Step Over (F10) until the program reaches line 89:

copy_of_list = item; // Set the end of our list to be our new item.

explanation of line 89, end of function cal

Observe what is about to happen. We are supposedly setting the end of copy_of_list to item. The problem is that item is not being linked to the previous item of copy_of_list, since the next pointer of the final item in copy_of_list is still NULL. As a result, no items are being appended to our original list. If you continue clicking Step Over (F10), copy_of_list becomes newItem, but does not append upon list.

Fixing the Error

Recall that linked list nodes point to the next node in the list.

linked list diagram coming soon

To fix the error, we need to set the end of our existing linked list's next pointer to our new node.

On line 85, change

while (copy_of_list != NULL) {               // Looping until we get to end of list.

to

while (copy_of_list->getNext() != NULL) {    // Looping until we get to end of list.

This will allow us to iterate through the list until we reach the final item (instead of continuing beyond the list).

mention debug again and identify other error ***reverse za

Next, let's have the final node in the list point to our new node.

On line 89, change

copy_of_list = item;            // Set the end of our list to be our new item.

to

copy_of_list->setNext(item);    // Set the end of our list to be our new item.

fixProblem

Wrapping Up

Run the program again, which should stop at the breakpoint we added on line 71. This time, observe the list variable as we Step Over (F10) the addLinkedListItemToRear() function. If you expand list, we can see that our new item was successfully added! You may continue to click Step Over (F10) to watch list fill up with items.

Finally, remove any remaining breakpoints and run the program one more time. The result should display the array and linked lists being equal. Congrats, you've fixed a common but important error in linked lists!

runProgram2