This lab is OPTIONAL. You do NOT need to do the lab, but
lab points for this lab can be used to make up for lab points lost
on other labs. You can also work on the optional
Word Ladder program.
The goals of this lab include doing the specific tasks outlined below
and understanding general concepts behind the tasks.
Lab 7 table of contents
In this lab you'll work with linked lists and linked lists
of linked lists (and doubly linked lists)
In large courses
students are usually
placed in smaller sections indicated by section numbers.
For organizational purposes, each section will be represented by a linked
list of students. The sections in a course
will also be linked together in a list.
A section is represented by a
SectionNode.
A SectionNode
stores the section number, a pointer to another section node,
and a pointer to a linked list of students. This struct contains a constructor
with default values for the pointers. A new
SectionNode can be constructed
with just one argument, an integer, and the two pointers will have default
values of NULL.
Information for a student is shown in a
StudentNode (shown below), which contains
the first and last name of the student, and a pointers to nodes after
and before the node (this makes the list of students a doubly-linked
list.)
A constructor and a function Print are declared in the struct.
If ptr points to StudentNode, then
ptr->Print() invokes the Print member function, printing the
name of the student in the node pointed to by ptr.
In the example below, the course cps100e has sections 2 and 3.
Section 2 has two people, section 3 has 1 person. Each list of students
has a dummy header node as shown.
In this section of the lab you'll copy files and then compile and run
the linked list demo program.
First change into your cps100e subdirectory (type pwd
to verify where you are). Create a
lab7 subdirectory by typing mkdir lab7 and change
into this subdirectory (be sure to check that you're
in the lab7 subdirectory.) Now copy the files for the lab
(don't forget the . when copying).
You should see the files listed below (these are links to the files in
case you use Netscape, and for users outside of Duke). This will copy
one data file of names (for people in 100e).
Be sure you're in the lab7 subdirectory, and check to see that
all files are there (type ls). Then, from an xterm window (at
the prompt [1] ola@teer8% or similar) compile the first
version of the program by typing: make sections.
This should compile several files and link them together
with the library libtapestry.a. The program reads from
standard input (cin). You'll redirect input so that the program reads
from a file by typing:
In this part of the lab you'll learn how to use ddd to find the reason
that sections crashes. You will fix the bug and check to make sure
that the program runs correctly.
To start the debugger, type
You should run the program: either type run < sec.dat inside the
command window. ddd
will run the program and
stop when the segmentation fault occurs.
There will be an arrow (kind of greenish in color) on the left side
of the source code window pointing at the line number that is causing the
segementation fault. This information, the name of the file, and the
line number is printed in the DDD command window (the command window is
running the gdb debugger, ddd is a graphical front end
to gdb. You can run gdb without the GUI front end
if you're on a computer without the ability to display X windows.)
Sometimes
(as in this case) the error appears to be in a function that you did not
write! But usually, the error is in your program, it was caught as you
were calling another function, so the execution halted inside the
other function.
To find where in the program sections.cc the error occured.
Type where in the command window or press
the corresponding button. When your program crashed,
there were many function calls on the system stack. They are shown
in reverse order. Somewhere near the bottom you'll see main,
line 157.
On this line, main called the function PrintList, whose call is shown
immediately above this line.
PrintList called
PrintSection which generated many recursive calls.
With each
call, the value of current is shown (current is a pointer, so its value
is some address shown in hexadecial).
In the last calll (top on the stack of calls),
current has the value 0x0 which
represents NULL at line 92 in the file sections.cc.
Take a look at line 92 in sections.cc (to move to a particular line,
type "C-x g" in emacs. If that doesn't work, type M-x goto-line,
that is, press the ESC key once followed by "x goto-line", then return,
and then a line number). The line is
What happens when current has the value NULL?
Then current->Print() doesn't make sense
since there is no node. When a NULL pointer is dereferenced
your program
crashes and gives you a segmentation fault. It might not crash right away.
In this case, as part of the dereferencing
the Print function tried to print a string, so it tried to use
the << operator in the string class, and it failed. That is why
the first error message is from the CPstring.h file.
In the debugger, you can display the values of variables for functions that
execution halts in. In your case execution halted in CPstring.h. It was called
by a long chaing of functions. Let's reverse steps in the chain
of function calls in
sections.cc. Type up in the command window or use the
corresponding button. Execution is moved
back to the function StudentNode::Print.
Use up again.
Execution is moved back to the function PrintList
when current had the
value 0x0 or NULL. You'll see something like:
Use up one more time, to the previous
function call of PrintList. This time current will have an address
other than 0x0. Since this current is pointing to something,
you can display values in its node. Click on the node and use the
display button to invoke the ddd display window.
You should be able to use ddd commands to find the first and
last names of the student --- the first name should be DOUGLAS, the last
name should be BROWN.
Fix the error above by placing an "if" check around the body of the
function PrintSection, executing
the body of only if current is not null. Replace the body by
Introduction
Compiling/Running sections
Debugging: Using ddd:
#3 ..... PrintSection (current=0x0) at sections.cc: 92
Summary of DDD buttons/commands
For more information, you can click on the Help option in the menu and look it up in the ddd manual.
To find out more, click on the Help button on the menu bar and consult the DDD manual.
After you've edited the program to fix the problem, recompile and re-run the program. You should also type ls to list your files. You may have a file called core that was created when your program had a segmenation fault. This file takes up lots of space and should be removed, type rm core to remove it.
The main function has one call to test whether or not WhichSection works correctly. You should add additional tests. Compile and run your program before moving on to the next part.
submit100e lab7.N README usestock.cc stlist.h stlist.cc
You can enter the files in any order.
Remember that every assignment must have a README file submitted with it (please use all capital letters). Include your name, the date, and an estimate of how long you worked on the assignment in the README file. You must also include a list of names of all those people with whom you collaborated on the assignment.