Question: Specification In order to understand variable scope and pass-by-value in C, in this exercise we trace a program using a software tool called debugger, rather

 Specification In order to understand variable scope and pass-by-value in C,
in this exercise we trace a program using a software tool called
debugger, rather than using print statements. A debugger allows us to examine

Specification In order to understand variable scope and pass-by-value in C, in this exercise we trace a program using a software tool called debugger, rather than using print statements. A debugger allows us to examine the values of variables during program execution. With a debugger, you can do this by setting several "breakpoints" in the program. The program will pause execution at the breakpoints and you can then view the current values of the variables. You will use a GNU debugger call gdb. It is a command-line based debugger but also comes with a simple text-based gui (tui). To debug a C program using gdb, you need to compile the program with -g flag of gcc. Implementation Note: for this exercise you may want to connect to lab environment, as you may not have the same debugger on your system. Download the program swap.c, and compile using gcc -g swap.c. Then invoke gdb by issuing gdb -tui a.out. And then press enter key. A window with two panels will appear. The upper panel displays the source code and the lower panel allows you to enter commands. Maximize the terminal and use arrow keys to scroll the upper panel so you can see the whole source code. First, we want to examine the values of variables mainA and mains after initialization. So we set a breakpoint at the beginning of line 11 (before line 11 is executed) by issuing break 11. Observe that a "b+" or "B+" symbol appears on the left of line 11. We want to trace the values of variables x and y defined in function swap, both before and after swapping, so we set breakpoints at the beginning of) line 18 and line 21. Finally we set a breakpoint at line 12 so that we can trace the value of mainA and mainB after the function call. When the program pauses at a breakpoint, you can view the current values of variables with the print or display or even printf command. Sample input/output red 64 gcc -g swap.c red 65 gdb -tui a.out Reading symbols from a.out... done. (gdb) break 11 Breakpoint 1 at Ox400488: file swap.c, line 11. (gdb) break 18 Breakpoint 2 at 0x4004a3: file swap.c, line 17. (gdb) break 21 Breakpoint 3 at 0x400465: file swap.c, line 21. (gdb) break 12 Breakpoint 4 at 0x400497: file swap.c, line 12. run the program until the (gdb) run Starting program: /eecs/home/huiwang/a.out first breakpoint. Notice the > sign on the left of the upper panel */ Breakpoint 1, main () at swap.c:11 (gdb) display maina mainA = ? What do you get for (gdb) display mainB mainA and mainB? mainB = ? (gdb) continue /* continue execution to the Continuing next breakpoint. Notice the position of > sign / Breakpoint 2, swap (x-1, y=20000) at swap.c:18 (gdb) display x X - ? What do you get (gdb) display y for x and y? y = ? (gdb) display mainA ... What do you get (gdb) display mainB for mains and .....? mains, and why? (gdb) continue Continuing Breakpoint 3, swap (x=20000, y=1) at swap.c:21 (gdb) display x X = ? What do you get for x (gdb) display y and y? Are they y - ? swapped? (gdb) continue Continuing. Breakpoint 4, main () at swap.c:12 (gdb) display mainA mainA = ? What do you get for mainA (gdb) display mainB and mainB? Are they mainB = ? swapped? (gdb) display x (gdb) display y What do you get here, and why? (gdb) quit ...? .....? #include void swap(int, int); int main() { int maina, mainB; mainA = 1; mainB = 20000; swap (mains, mainB); return 0; } void swap (int x, int y) { int temp = x; x = y; y = temp; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!