This originally was in a text file, gdb.txt. It was
moved into an HTML document so comments could be embedded in a red font.
The code is given below (but can also be seen in gdb by using the list command).
// Swap two char variable values
// allowing for repeats
// Example for demo of gdb
//JWB
#include <iostream>
using namespace std;
int main () {
int i;
char a,b, more;
do {
cout << "Input character values for a and b : ";
cin >> a;
cout << "\nOriginally a is " << a << " and b is " << b << endl;
//do swap
a = b;
b = a;
//output results
cout << "\nAfter the swap a is now " << a << " and b is now " << b
<< endl;
cout << "Input c to continue; s to stop : ";
cin >> more;
cout << endl;
}
while (more = 'c');
}
Originally a is A and b is ¿ Oops- b doesn't seem to have a value
After the swap a is now ¿ and b is now ¿
Value of a seems lost
Input c to continue; s to stop :
Program doesn't wait for input here
Input character values for a and b : X Y Note- it loops without any input
Originally a is X and b is ¿ Same
problems occur
After the swap a is now ¿ and b is now ¿
Input c to continue; s to stop :
Input character values for a and b :
Program received signal SIGINT, Interrupt. CTRL-C
to abort the run
0x006db410 in __kernel_vsyscall ()
(gdb) l List the lines
at the beginning
1 // Swap two char variable values
2 // allowing for repeats
3 // Example for demo of gdb
4 //JWB
5 #include <iostream>
6 using namespace std;
7 int main () {
8 int i;
9 char a,b, more;
10 do {
11 cout << "Input character values for a and b : ";
(gdb) l List more of the program
12 cin >> a; Oops- looks
as if b is not input
13 cout << "\nOriginally a is " << a <<
" and b is " << b << endl; Break
here
14 to
examine a and b
15 //do swap
16 a = b;
17 b = a;
18
19 //output results
20 cout << "\nAfter the swap a is now " << a << " and b is now " <<
b << endl;
21
(gdb) b 13 Set a break at line 13
Breakpoint 1 at 0x80487f0: file swap.cpp, line 13.
(gdb) r Redo the run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /users/cs/faculty/jbaker/cs1-alt/swap
Input character values for a and b : AB
Breakpoint 1, main () at swap.cpp:13
13 cout << "\nOriginally a is " << a << " and b is " << b <<
endl;
(gdb) p a Look at value for
a-it's ok
$1 = 65 'A'
(gdb) p b Look at value for b-
$2 = -65 '¿' it's incorrect
(gdb) p b='B' Ignore input and set b
$3 = 66 'B'
(gdb) c
Continuing. Continue the run
with the new value
Originally a is A and b is B Now this output
is correct
After the swap a is now B and b is now B OOps-swapping
code must be wrong
Input c to continue; s to stop : This line is not
excuting
Input character values for a and b :
Program received signal SIGINT, Interrupt.
0x00829410 in __kernel_vsyscall () Abort
(gdb) l
8 int i;
9 char a,b, more;
10 do {
11 cout << "Input character values for a and b : ";
12 cin >> a;
13 cout << "\nOriginally a is " << a << " and b is " << b <<
endl;
14
15 //do swap
16 a = b; Tracing this by hand we see a is set to b's value
17 b = a; B
and b is set to a's value which is B
We see a is set to B and b
is set to a's value which is B
(gdb) l
18
19 //output results
20 cout << "\nAfter the swap a is now " << a << " and b is now " <<
b << endl;
21
22 cout << "Input c to continue; s to stop : ";
23 cin >> more;
24 cout << endl;
25 }
26 while (more = 'c');
27 }
(gdb) b 24
(gdb) clear 13
Deleted breakpoint 1
(gdb) b 24 Set a breakpoint at line 24 to see
why the loop is not handled correctly
Breakpoint 2 at 0x80488d3: file swap.cpp, line 24.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /users/cs/faculty/jbaker/cs1-alt/swap
Input character values for a and b : AB
Originally a is A and b is ¿
After the swap a is now ¿ and b is now ¿
Input c to continue; s to stop :
Breakpoint 2, main () at swap.cpp:24
24 cout << endl;
(gdb) p more Check value of more
$4 = 66 'B' This was the value for b which was
not stored earlier in the program
(gdb) p more='c' Set more to 'c'
(gdb) c
Continuing.
Input character values for a and b : So, it looks
as if the loop is ok- or is it?
Program received signal SIGINT, Interrupt.
0x00293410 in __kernel_vsyscall ()
(gdb) quit
The program is running. Exit anyway? (y or n) y
[loki.cs.kent.edu]{3}-> exit
Script done on Mon 07 Sep 2009 12:55:39 AM EDT
Observe we have found the following errors:
1) b does not receive a value
2) the swap code needs to be corrected.
By correcting 1) we also correct the failure to ask the
user to decide whether or not to continue.
However, we did not try to run when more is 's'. There is an error there as more = 'c' in the bottom while statement
causes more to be ASSIGNED the code for 'c' and being nonzero, it is true even when more is 'c'.To correct, change
to more == 'c'.
This is a good lesson in realizing that debugging is one of the hardest parts of programming - but also a fun part
as you are acting like a detective. Don't let your ego get in the way - everyone has errors in their programs initially.