Lab 5
2020_S19/wk5/lab5.cpp
2020_S19/wk5/lab5.txt
The purpose of this lab is to use the GNU Debugger (GDB) and practice
debugging programs that contain compilation warnings and runtime errors.
Also, practice polymorphic behavior, an abstract base class with pure
virtual member functions.
Follow the steps provided and create a lab5.txt file that contains
your responses to the given lab.
wget http://www.cs.csubak.edu/~derrick/cs2020/examples/shape_withErrors.cpp .
Copy the given shape_withErrors.cpp and compile the source code
using the -g flag to enable debugging information and -Wall to show
all warnings.
g++ -g shape_withErrors.cpp -Wall
- What is the warning that g++ gives us?
- What is the solution to fix the warning? (hint: destructor)
Your program right now is able to execute, however, warnings
are still errors!
Recompile the program after fixing the warning, compile, and run the
program.
- A segmentation fault should occur. In your own words (with out debugging),
why might you think a segmentation fault occurred?
gdb executable
Load the executable into gdb (a.out or however you might haved named your executable
if given the -o flag with g++) and set a break point at function main
(break main
). Next, run the program by typing run
and step
through the program, and enter a choice when prompted.
- What is the GDB output when the Operating System receives a signal
SIGSEGV?
- Which function invokes the segmentation fault?
Re-run the program again in the GDB environment, and step though again.
Upon the first instance of GDB notifying us that a SIGSEGV signal has been
received, use the info locals
command.
- What is the output of 'info locals'?
- What is the clue that suggests why a segmentation fault occerred?
Exit GDB by typing quit
. Edit the code and fix the segmentation
fault by allocating the derived object that corresponds to the
menu options. Compile and run the program.
Open GDB again, and type info functions
. Set
a break point to main and Shape::inputShape. Step through the program,
enter option 1 for Square, and step
until you reach the second breakpoint at 'inputShape'. Use
command print this
and ptype this
.
- What is the output for
print this
and ptype this
?
- Which version of inputShape and printShape is called,
compared to earlier?
This happens because we are using a base class pointer
to reference a derived object, however the functions that are invoked are
only the base class's due to static linking/binding.
Add the necessary keyword to the base
class member function prototypes to allow for polymorphic
behavior and dynamic binding.
Open GDB again, and type info functions
. Set
a break point to main and Square::inputShape. Step through the program,
enter option 1 for Square, and step
until you reach the second breakpoint at 'inputShape'. Use
command print this
and ptype this
.
- What is the output for
print this
and ptype this
for option 1?
Re-run the pogram with run
and 'y' to start at th beginning.
Type info breakpoints
and delete the
breakpoint for Square::inputShape() with delete 2
and set a new breakpoint for Rectangle::inputShape(). Repeat the process
with option 2 and use command print this
and
ptype this
. Take notice to the output and datatype this is
referencing.
- What is the output for
print this
and ptype this
for option 2?
Continue the program if you used option 1 or 2 to enter a Shape object,
then chose option 0.
Another segmentation fault should occur. This segmentation fault occurs
whenever you attempt to deallocate already freed memory (double free error).
Add an if statement at the end of our menu based loop that checks to see if
your Shape pointer is not null, if true, deallocate the memory and set
your Shape pointer to null.
In addition, add the same if statment outside of the loop before
we close our program. It's always good practice to check, deallocate,
and handle dangling pointers. Recompile and run again.
- By now your source code should be error free and memory leak free
and allows for polymorphic behavior. Use the unix copy command and
copy your source code and call it 'lab5.cpp'
cp ./shape_withErrors.cpp ./lab5.cpp
- Edit your lab5.cpp so that your Shape Class is an
Abstract Base Class by making inputShape and
printShape pure virtual member functions (=0). Remove the
Shape::inputShape and Shape::printShape definitions. Compile lab5.cpp
and run the program.
Have your completed lab5.cpp source code and lab5.txt answers:
2020_S19/wk5/lab5.cpp
2020_S19/wk5/lab5.txt