Starting from:

$34.99

CS1010S Lab 4 Solution

Using the Visual Studio Code Debugger
Introduction
Obtaining the files
1. Obtain the zip file for the lab from Coursemology. You will be given a similar zip file for the PE.
2. Extract the contents of the zip file to a directory, e.g. Desktop or Documents.
• For Mac, double clicking the zip file will extract the contents to the current folder.
• For Windows, double clicking will open a window to examine the zip file. You will have to drag the folder out to extract the contents.
You should now see a folder called lab04 containing at least two files: lab04.cpp and lab04.code-workspace .
Note for MacOS
To disable SIP:
1. Restart your Mac into Recovery mode, by holding down + R from when it restarts until you see the Apple icon and a progress bar.
2. From the Utilities menus, select Terminal.
3. At the terminal prompt, type csrutil disable exactly and press Enter. The terminal should display a message saying that SIP was disabled.
4. From the Apple menu, select Restart.
SIP can be re-enabled by following the same steps, but entering csrutil enable instead.
Starting Visual Studio Code
lab04.cpp
lab04.code-workspace
Normally you will open the file using Visual Studio Code. But in order to use the debugger, you will instead open the file using Visual Studio Code instead.
1
If double-clicking on the file does not work, simply select File from the menu in Visual Studio Code, and Open Workspace... and open the file.
You should see the lab04.cpp file open for editing in VS Code:

Running the debugger
Switch the left pane to the debugger by clicking on the icon. At the top you should see a Run button, with two options: Debug for MAC and Debug for Windows.

Click on the appropriate option. These configurations should work if you had followed the setup given in Problem Set 0. Otherwise, you might have to modify them yourself.
Your code will be compiled and run, and the execution should be paused with the very first step highlighted.

The debugging view
The left pane shows the memory state of the program at the current step. On the code pane, the current instruction to be executed is highlighted. Since the variables are not yet initialized, we see they contain random values in memory.
Somewhere at the top there is a controls bar (that can be repositioned by dragging). Mouse over the buttons will reveal that they are Continue, Step Over (F10), Step Into (F11), Step Out (Shift+F11), Restart and Stop.
1. Click or press the Step over icon in the debugging control panel. This will advance the program execution by one step. You should see that the variables have been correctly initialized.

2. Now the next line to be executed is function call to sort . If you were to click Step over button again, the execution will “step over” that line by performing the function call without pausing. Try it.
You will notice that the variables did not change at all after the function call. Something must be wrong in the function.
3. So restart the debugger and come back to the previous step. This time, click or press Step into the function call.

Notice that the execution is now at the sort function and our variables are now replaced with the inputs a , b and c .
4. Now use the Step over or Step into buttons to control the execution of the program and view the memory state changes.
5. If you do not want to continue tracing through a function, or you stepped into a function in the C++ standard library, you can return using two ways. The first is to press Step out to continue execution until it steps out of the current function. The second is to put a breakpoint on a line of code and press Continue. The program will execute until it hits a breakpoint.
6. To put a breakpoint, click on the left of the line number of the line where you want the execution to pause. A red circle should appear, indicating a breakpoint has been created.

Fixing the code
There are some mistakes (bugs) in this code. Now try using the debugger to find what is wrong and fix it.

More products