COMP 2673 - Lab8 In-lab Assignment Goals: Debugging using ddd - After this lab you should know: compilation flags to be used while debugging and after debugging where to set breakpoints when to use Step, Next and Continue controls how and where to examine the values of variables To Do: Go through the following steps and also answer the questions. Remember to email your code to your TA at the end. Steps: 1. Change your present working directory to IntroToCS3. 2. Create a new directory lab8. 3. Change your present working directory to lab8. 4. Copy the file ~sarora/IntroToCS3/lab8/linkedlist2.tgz to your directory. 5. Extract the assignment 8 files from this file using the command: tar -xvzf linkedlist2.tgz 6. The above command will create a directory "linkedlist2". 7. Enter this directory. 8. Compile the files by typing "make". This creates an executable file by the name "executable". "executable" is a program that performs addition, deletion and traversal on linked lists, and it has some bugs that we will locate and fix using debugger in today's lab. 9. If you look in the Makefile, the files have been compiled using the "-g" flag. Question 1> When is the "-g" flag used for compilation and why? Answer: 10. Read the file SLList.h to see the description for tasks done by the member functions of class SLList. 11. Type executable at the command prompt to run the program. You would notice that Test 4 and Test 5 are not working correctly. Question 2> In what way is the result of Test 4 different from the desired result? Answer: Question 3> In what way is the result of Test 5 different from the desired result? Answer: 12. Open the file in the debugger by typing "ddd executable &". This would open the source file (corresponding to the executable) which contains the function "main()". In this case it will open "llist.cpp" in the ddd window (because "llist.cpp" is the file corresponding to "executable" that contains the function "main()"). 13. Click close on the dialog box "DDD Tip of the day". 14. Set a breakpoint on the first line inside the function main(). (It is generally a good idea to put a breakpoint on the first line inside the function main() so that you can see the flow of control from the start.) 15. Also set a breakpoint on the line where Test 4 is being performed. (To see why it is working incorrectly.) If there are more than one errors in a program, always try to debug the error occouring from statements where the flow of control reaches earlier, as the error might propogate in the program making it difficult to debug other errors. 16. In the Program Menu (in top menu bar), check the item "Run in Execution Window". This will allow you to see the result of execution in a separate "Execution Window" as the program runs. Start running the program in the debugger by pressing F2 and then in the dialog box which appears, click "Run". 17. Press 3 to view the data window. 18. Press 8 to view the command tool. 19. Execute the program in the debugger using the following commands: - Click "Step" in the command tool window to "step inside" the statement. - Click "Next" in the command tool window to run the next statement. - Click "Cont" in the command tool window to continue execution till the the next "termination point", which is either a Breakpoint you have set, a fatal error in the program or the end of program. - Toggle a breakpoint by moving the cursor to a line and using the "stop" button in the menu. 20. Once you reach the line where you think the delete operation is performed, click "Step" in the command tool window to "step inside" the statement. 21. A variable can be displayed in Data window in 2 ways, as described below. 22. Right click on the variable name "corpse" and click "Display corpse". 23. The value of variable "corpse" shows up in the data window. 24. Right click on this value and select "Display *()". 25. The value of location pointed to by "corpse" shows up in the data window. 26. In the data window, right click and select "New Display...". 27. In the dialog box for "New Display" type "temp" and click "Display". 28. The value of variable "temp" shows up in the data window. 29. Right click on this value and select "Display *()". 30. The value of location pointed to by "temp" shows up in the data window. 31. Click "Next" in the command tool window and watch the data window to observe any change in the value of variables. 32. Repeat the above step till the control reaches just before the last line of this member function. 33. In the data window, right click and select "New Display...". 34. In the dialog box for "New Display" type "temp->Next" and click "Display". 35. The value of variable "temp->Next" shows up in the data window. Question 3> What is the value of "temp->Next"? Answer: Question 4> If you read the code of this member function, you will see that "temp" points to the element just before the one that is to be deleted (i.e. "corpse"). After successful deletion of "corpse", where should "temp->Next" point to? Answer: Question 5> If your answer of Question 3 is different from your answer of Question 4, which line of code do you think is causing this inconsistency? Answer: 36. Change this line (Answer of Question 5), so that Test 4 runs correctly. 37. Compile the files again by typing "make" and run the "executable" again to see if the error got fixed. Question 6> In the data window, the value of some variables gets disabled when control reaches certain parts of the program and shows up again later. Why do you think this happens? Answer: 38. (EXTRA CREDIT) In the debugger window, open the updated version of the code (File Menu (in top menu bar) -> Open Recent -> executable) and "step inside" each statement of Test 5 to figure out and fix the bug. 39. Quit the debugger by pressing Q. 40. Remember to remove the "-g" compilation flag once the executable is working correctly. Use the "-O3" optimization flag instead. Question 6> Why is the "-g" flag removed once the executable is working correctly? Answer: 41. Email the corrected version of file SLList.cpp to your TA.