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

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Dynamic Memory Allocation (also see pointers lectures) -L. Grewe.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Introduction to Programming Lecture 39. Copy Constructor.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Object Oriented Programming COP3330 / CGS5409.  C++ Automatics ◦ Copy constructor () ◦ Assignment operator =  Shallow copy vs. Deep copy  DMA Review.
CSE 332: C++ memory management idioms C++ Memory Management Idioms Idioms are reusable design techniques in a language –We’ll look at 4 important ones.
Debugging What can debuggers do? Run programs Make the program stops on specified places or on specified conditions Give information about current variables’
Chapter 15 Memory Management: Four main memory areas for a C++ program: Code: code for instructions, methods, etc. static data: Global variables (declared.
Object Oriented Programming.  OOP Basic Principles  C++ Classes  September 2004  John Edgar 22.
Classes Separating interface from implementation
DEBUGGERS For CS302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
OOP Languages: Java vs C++
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Copy Control Joe Meehean. More Class Responsibilities When making a new type (i.e., class) we must specify what happens when it is: Copied Assigned Destroyed.
Memory & Storage Architecture Seoul National University GDB commands Hyeon-gyu School of Computer Science and Engineering.
CS 11 C++ track: lecture 4 Today: More on memory management the stack and the heap inline functions structs vs. classes.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Data Structures - CSCI 102 CS102 C++ Pointers & Dynamic Objects Prof Tejada.
Memory Management Issues, Solutions, and Examples.
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
 Managing the heap  Resource acquisition is initialization (RAII)  Overriding operator new and delete  Class-based memory pools.
Dynamically Allocated Arrays December 4, Skip the Rest of this PowerPoint.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Pointer and Array Lists Chapter 3, Summary CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and.
More C++ Features True object initialisation
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CMSC 202, Version 3/02 1 Copy Constructors and Overloaded Assignment.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 1.
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
1 // SPECIFICATION FILE (dynarray.h) // Safe integer array class allows run-time specification // of size, prevents indexes from going out of bounds, //
Memory Management in Java Mr. Gerb Computer Science 4.
Lab 2 main issues Point and line classes should NOT have their own protected “type” attributes these hide the parent (shape) classes attribute with the.
Memory Management.
Copy Constructor / Destructors Stacks and Queues
Programming with ANSI C ++
C++ Memory Management Idioms
Class: Special Topics Copy Constructors Static members Friends this
Memberwise Assignment / Initialization
This pointer, Dynamic memory allocation, Constructors and Destructor
Dynamically Allocated Memory
Automatics, Copy Constructor, and Assignment Operator
understanding memory usage by a c++ program
Automatics, Copy Constructor, and Assignment Operator
Constructors and destructors
CSc 352 Debugging Tools Saumya Debray Dept. of Computer Science
Destruction and Copying
Indirection.
Essential Class Operations
Destructor CSCE 121 J. Michael Moore.
Destructor CSCE 121.
Destruction and Copying
CS410 – Software Engineering Lecture #5: C++ Basics III
COP 3330 Object-oriented Programming in C++
Class: Special Topics 2 For classes using memory allocation
Essential Class Operations
Rule of Three Part 1 & 2.
Copy Constructors and Overloaded Assignment
SPL – PS3 C++ Classes.
Presentation transcript:

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  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

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

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