Example code to debug:
//Account.h
//Definition of Account class
class Account {
public:
Account(int);
void credit(int); //add to account
void debit(int); //subtract from account
int getBalance();
private:
int balance;
};
//Account.cpp
#include <iostream>
using std::cout;
using std::endl;
#include "Account.h"
Account::Account(int initialBalance) {
balance = 0;
if(initialBalance > 0)
balance = initialBalance;
if(initialBalance < 0)
cout<<"Error - Initial balance cannot be negative.\n"<<endl;
}
void Account::credit(int amount) {
balance += amount;
}
void Account::debit(int amount) {
if(amount <= balance)
balance -= amount;
else
cout<<"Debit amount exceeded account balance.\n"<<endl;
}
int Account::getBalance(){
return balance;
//AccountApplication.cpp
//Create and manipulate accounts
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include"Account.h"
int main() {
Account account1(50);
cout<<"account1 balance": $"<<account1.getBalance()<<endl;//line 12
int withdrawalAmount;
cout<<\nEnter withdrawal amount: ";
cin>>withdrawalAmount;
cout<<"\nattempting to subtract " <<withdrawalAmount
<<" from account1 balance\n\n";
account1.debit(withdrawalAmount); //line 18
cout<<"account1 balance: $"<<account1.geBalance()<<endl;
return 0;
}
A debugging session:
$ g++ -g -o AccountApp Account.cpp AccountApplication.cpp
note: the -g option adds debugging code to the output
$ gdb
You can set values suing the set command:
(gdb) run
Starting program: /home/jones/AccountApp
account1 balance: $50
Enter withdrawal amount for account1: 13
attempting to subtract 13 from account1 balance
account1 balance: $37
Program exited normally
(gdb)
(gdb) break 12
Breakpoint 1 at (. . .) line 12.
(gdb) break 18
Breakpoint 2 at (. . .) line 18.
(gdb) run
Starting program /home/jones/AccountApp
Breakpoint 1, main() at AccountApplication.cpp:12
17 cout<<"account1 balance: $"<<account1.getBalance()<<endl;
(gdb) continue
Continuing.
Enter withdrawal amount for account1: 13
attempting to subtract 13 from account1 balance
Breakpoint 2, main() at AccountApplication.cpp:18
25 account1.debit(withdrawalAmount);
(gdb) print withdrawalAmount
$2 = 13 //$n variables are convenience variables
(gdb) print account1 //prints the object's data values
$3 = {balance = 50}
(gdb) print $1
$3 = 13
(gdb) continue
Continuing.
account1 balance: $37
Program exited normally.
(gdb)info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x08048 in main at AccountApplication.cpp:12
breakpoint already hit 1 time
2 breakpoint keep y 0x0804e in main at AccountApplication.cpp:18
breakpoint already hit 1 time
(gdb) delete 1
(gdb) delete 2
(gdb) info break
No breakpoints or watchpoints
(gdb)quit
$
Print and set Commands:
print withDrawalAmount -2 print withdrawalAmount == 11 will return a boolean value of false
(gdb) set withdrawalAmount = 42 (gdb) print withdrawalAmount $3 = 42 (gdb)continue Continuing. account1 balance: $8 Program exited normally (gdb)quit $
Using step, next, finish, and watch commands:
Note:
(gdb) break 14 Breakpoint 1 at . . . (gdb) run Starting program: . . . Breakpoint 1, main() at . . . . :14 14 Account account1( 50); (gdb) watch account1.balance Hardware watchpoint 2:account1.balance (gdb)next Hardware watchpoint 2: account1.balance Old value = 0 New value = 50 Account (this = 0xnnnnn, initialBalance=50) at Account.cpp:20 20 if(initialBalance < 0) (gdb)continue Continuing account1 balance: $50 Enter withdrawal amount for account1: 13 attempting to subtract 13 from account1 balance Hardware watchpoint 2: account1.balance Old value = 50 New value = 37 0xnnnnn in Account::debit (this=0xnnnnn, amount - 13) at Account.cpp:34 34 balance -= amount; (gdb)continue Continuing account1 balance: #37 Watchpoint 2 deleted because the program has left the block in which its expression is valid. 0xnnnn in exit () from . . . . . (gdb) continue Continuing. Program exited normally. (gdb)
Note: if you ‘run’ the program again in the debugger you will have to reset the watch on the variable
(gdb) run Starting program . . . Breakpoint 1, main() at ...... :14 14 Account account1(50); (gdb) watch account1.balance Hardware watchpoint 3: account1.balance (gdb) continue Continuing. Hardware watchpoint 3: account1.balance Old value = 0 New value = 50 Account (this=0xnnnnnn, initialBalance=50) at Account.cpp:20 20 if(initialBalance < )) (gdb) delete 3 //deletes the watchpoint (gdb) continue . . . Program exited normally (gdb) quit