Debugging C++ Applications Using HP WDB (766162-001, March 2014)
elementary operations, such as copying and assignment. STL algorithms are independent of
containers, which significantly reduces the complexity of the library. HP WDB enables you to debug
a C++ application that uses STL. It is possible to call STL library function calls from the WDB
command line by compiling the C++ application with the +d compilation option. The +d compilation
option ensures that the functions do not expand inline, which is important while debugging STLs.
Example 23 shows a sample program for debugging STL.
Example 23 Sample Program for Debugging STL
1 #include <iostream>
2 #include <vector>
3 using namespace std;
4
5 int main() {
6 vector<int> v(3); // Declare vector holding 3 elements
7 v[0] = 5;
8 v[1] = 2; // Assign elements
9 v[2] = 7;
10 vector<int>::iterator first = v.begin(); // Get iterator
11 vector<int>::iterator last = v.end();
12 vector<int>::reverse_iterator rlast = v.rend();
13 while (first != last)
14 {
15 cout << *first++ << " "; // Print out contents
16 }
17 }
The WDB output snippet for this program is as shown below:
(gdb)
13 while (first != last)
(gdb) l
8 v[1] = 2; // Assign elements
9 v[2] = 7;
10 vector<int>::iterator first = v.begin(); // Get iterator
11 vector<int>::iterator last = v.end();
12 vector<int>::reverse_iterator rlast = v.rend();
13 while (first != last)
14 {
15 cout << *first++ << " "; // Print out contents
16 }
17 }
(gdb) p v
$1 = {_C_start = 0x400250e0, _C_finish = 0x400250ec,
_C_end_of_storage = 0x40025160}
(gdb) p v[0]
$2 = (int &) @0x400250e0: 5
(gdb) p v[1]
$3 = (int &) @0x400250e4: 2
(gdb) p v[3]
$4 = (int &) @0x400250ec: 0
(gdb) p v.begin()
$5 = (int *) 0x400250e0
(gdb) p *(int*)v.begin()
Debugging standard template library (STL) 35