CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD

Download Report

Transcript CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD

CS 225 Lab #2 - Pointers, Copy Constructors,
Destructors, and DDD
Introduction to Copy Constructors



A special kind of constructor in C++
Creates a new object as a copy of an existing one
There exists an implicit copy constructor in all
classes in C++ that do not define one


Unfortunately, it only creates a shallow copy of all
the data members
This will create problems when a class has data that
involves pointers to dynamic memory
Idea of the Copy Constructor

Shallow copy – used to copy direct values


Deep copy – used to copy indirect values


If you make a shallow copy of a pointer, you would
have two pointers that point to the same location
If you make a deep copy of a pointer, you will have
two pointers that point to different locations but
those locations have the same value
Modifying the original object should not affect
its copy or vice versa
Using a Copy Constructor

Explicit Use:



Implicit Use #1:



Building a(4); // standard constructor call
Building b(a); // b is now a deep copy of a
Building a(4); // standard constructor call
Building b = a; // implicit call to the copy
constructor
Implicit Use #2:

When an object is returned by value from a function
Using a Copy Constructor (2)

Common Misunderstanding:





Building a(4); // standard constructor call
Building b(3); // standard constructor call
b = a;
// does NOT call copy constructor
The copy constructor is never called in this code
The last line uses the assignment operator which
may be overloaded in C++
Introduction to Destructors



A special function that may be explicitly called,
but it is automatically invoked when an object is
destroyed
Its purpose is to free up any resources that were
acquired by the object
Proper use of a destructor will help prevent
memory leaks!
Use of a Destructor

For an object created on the stack:


the destructor is automatically invoked when the
object goes out of scope
For an object that was dynamically allocated on
the heap:

the destructor called when the “delete” command is
used on the object
Debugging



Oh noes!!! My program does not work.
Debugging is how you figure out where a
problem is in your code
Can be done with



cout statements
IDE's
Debuggers (gdb, ddd, etc.)
Introduction to Debuggers




Debuggers are an essential tool that any
computer scientist should know how to use.
They control the execution of a program
Breakpoints are used to stop execution
Status of memory can be checked at any desired
instance of a program's execution
Data Display Debugger (DDD)



DDD is a free GUI for command line debuggers
such as GDB
It provides a much simpler interface than GDB
DDD features interactive data display


data structures are displayed as graphs
A tool that you will find useful for this class and
beyond
Firing up DDD




First write, compile, and link the code you wish to
debug
If your binary executable is called foo, then you can run
DDD on foo by typing: “ddd foo” into the console
Note that the binary executable should be in your
working directory
Also note that the binary executable must have been
compiled with the debug option “-g”
Using DDD

Important Buttons:




Run: begins program execution
Step: continues execution to the next line of code
(following any function calls)
Next: continue execution to the next line of code
(skipping over any function calls)
Remember to set at least one breakpoint
Using DDD (2)




Debugging involves starting and stopping the
execution of the program and peeking at the
values of data in memory
Use the Display command to display data as a
graph
Display Local Variables (Alt + L)
Layout Graph (Alt + Y)