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 the list of filesystems,
and prints out the line that starts with “proc”. It has several errors
(don’t rob others of the joy of discovery—keep it to yourself):
(:source lang=cpp filename=bad.cc -inline:)
using namespace std;
int main() {
string filename = "\etc\fstab"; // A stupid line just to introduce an error: cout << "The 3rd character is " << filename[100000003] << '\n'; ifstream in(filename.c_str()); assert(in.is_open()); string s; while (getline(in, s)) { string prefix = s.substr(1,4); if (prefix == "proc") { cout << s << '\n'; } } return 0;
} (:sourceend:)
When the program works, it prints something like this:
proc /proc proc defaults 0 0
bad.cc
.
g++ bad.cc
filename[100000003]
g++ -D_GLIBCXX_DEBUG bad.cc
[100000003]
to [3]
.
g++ -DNDEBUG bad.cc
g++ -ggdb bad.cc
gdb ./a.out
b 15
(your line number may vary)
r
filename
, like this: p filename.c_str()
filename
has that value. Fix the source.
if (prefix == "proc")
line.
s
and prefix
the same way that you printed the value of filename
.
For extra fame & glory:
Modified: 2009-01-31T21:33 User: Guest Check: HTML CSSEdit History Source |
Apply to CSU |
Contact CSU |
Disclaimer |
Equal Opportunity Colorado State University, Fort Collins, CO 80523 USA © 2015 Colorado State University |