In this lab, you will learn to use debugging tools such as gdb,
assert, and -D_GLIBCXX_DEBUG
.
                
Consider the following program, bad.cc
. It opens resolv.conf
,
and prints out the line that starts with “search”. It has several errors
(don’t rob others of the joy of discovery—keep it to yourself):
                
#include <fstream> #include <iostream> #include <string> #include <vector> #include <cassert> using namespace std; int main() { // First, some stupid code just to introduce an error: vector<int> v = {2,42,165,253,1003}; cout << "My favorite number is " << v[10000003] << '\n'; string filename = "\etc\resolv.conf"; ifstream in(filename); assert(in.is_open()); string s; while (getline(in, s)) { string prefix = s.substr(1,6); if (prefix == "search") cout << s << '\n'; } return 0; }
When the program works, it prints something like this:                 
search cs.colostate.edu colostate.edu
bad.cc
. g++ -Wall bad.cc
v[100000003]
g++ -Wall -D_GLIBCXX_DEBUG bad.cc
[100000003]
to [3]
.
g++ -Wall -DNDEBUG bad.cc
g++ -Wall -ggdb bad.cc
gdb ./a.out
b 16
(your line number may vary)
r
filename
, like this: p filename
filename
has that value. Fix the source.
if (prefix == "search")
line.
s
and prefix
the same way that you printed the value of filename
.
For extra fame & glory:                 
User: Guest