Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD."— Presentation transcript:

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

2 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

3 Idea of the Copy Constructor Shallow copy – used to copy direct values  If you make a shallow copy of a pointer, you would have two pointers that point to the same location Deep copy – used to copy indirect values  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

4 Using a Copy Constructor Explicit Use:  Building a(4);// standard constructor call  Building b(a);// b is now a deep copy of a Implicit Use #1:  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

5 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++

6 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!

7 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

8 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.)

9 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

10 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

11 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”

12 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

13 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)‏


Download ppt "CS 225 Lab #2 - Pointers, Copy Constructors, Destructors, and DDD."

Similar presentations


Ads by Google