Bonus Lab 2: Debugging with Eclipse
Topics
- Revision on linked lists
- Introduction to Eclipse C++ debugger
Download
List Access Problem
In this problem, we are given a linked list of city names.
Together with a sequence of requested accesses:
- Sydney, Rome, Rome, Rome, Rome, HongKong, Moscow, ...
- The access cost of a city is defined as the number of cities that have to be traversed before getting to that city.
- Given a sequence of requested accesses, our objective is to minimize the cost of accessing the cities in the sequence according to the given order.
- While processing the accesses, we are allowed to modify the list.
- After accessing a city in the list, we may move it anywhere closer to the front of the list.
Algorithms
Transpose
- After accessing city 'A', move city 'A' one unit forward towards the front of the list.
- E.g., after accessing Sydney, the list becomes:
Move-to-Front (MTF)
- After accessing city 'A', move city 'A' to the front of the list.
- E.g., after accessing Sydney, the list becomes:
- Function overloading allows functions to have the same name but with different numbers of parameters and/or parameter types.
- However, specifying default arguments is a better option than using overloading.
- All parameters that do not specify default values MUST appear before the default arguments.
Debugging with Eclipse
- Create a makefile project named "listaccess"
Copy the files into the project "listaccess". Note that the project is buggy.
Click the build button (i.e. the hammer icon):
Compile the project and get ready to use the debugger.
Choose the gdb debugger from the dialog:
Allow Eclipse to switch to the Debug Perspective (The default is C++ Perspective). Debug Perspective is useful for debugging, and you can switch back to C++ Perspective after the debug process:
A console is opened when the project is executed.
The program halts at the default breakpoint and we will do step-by-step debugging. Choose Step Over or press F6 from the keyboard
The program halts. A bug is spotted.
Set a breakpoint at the problematic line. Right click the line and choose toggle breakpoint. A breakpoint icon is added on that line.
Terminate (by clicking the stop button) and then Launch (by clicking the debug button) the debugging process again.
The program halts at the default breakpoint. Resume it.
Go to the buggy line (breakpoint) and "step into" it. Step into will enter the function and trace the codes inside the function.
In the buggy function List::Delete do step-by-step debugging (using step over)
Reach an infinite loop (the bug we are looking for). Think about what we have missed!
Switch back to the C++ Perspective after the debugging process
Lab Task
- Create a project "listaccess" in Eclipse.
- Download and unzip listaccess.zip.
- Debug the function List::Delete() in Eclipse.
- Demo to the TA how you create breakpoints
- Explain the TA what is the different between Step Into and Step Over
- Add a function MoveToFront in the "List" class. A C++ string will be used as the input parameter. The selected node will be moved to the head of the linked list. The order of other nodes is remain unchanged.
- Modify the main.cpp
- Changes the line list.Transpose(strCity); to list.MoveToFront(strCity);
- “Syndey” is used to illustrate the concept of MoveToFront:
Demo to the TAs: (Bold is the input)
Before the modifications |
After the modifications |
List Accessing Problem Type Cities to Access and end with '#' (q to exit) e.g. London HongKong Paris ... HongKong # HongKong Paris NewYork Sydney London Rome Moscow Hamburg SaoPaulo Seoul Cities > Seoul Seoul Seoul # Cost:27 HongKong Paris NewYork Sydney London Rome Seoul Moscow Hamburg SaoPaulo |
List Accessing Problem Type Cities to Access and end with '#' (q to exit) e.g. London HongKong Paris ... HongKong # HongKong Paris NewYork Sydney London Rome Moscow Hamburg SaoPaulo Seoul Cities > Seoul Seoul Seoul # Cost:12 Seoul HongKong Paris NewYork Sydney London Rome Moscow Hamburg SaoPaulo |