Assignment of Structs

Similar documents
Assignment of Objects

Object-Oriented Principles and Practice / C++

Outline. Dynamic Memory Classes Dynamic Memory Errors In-class Work. 1 Chapter 10: C++ Dynamic Memory

CSC1322 Object-Oriented Programming Concepts

Classes and Objects. Class scope: - private members are only accessible by the class methods.

See the CS 2704 notes on C++ Class Basics for more details and examples. Data Structures & OO Development I

Object-Oriented Principles and Practice / C++

CPSC 427: Object-Oriented Programming

Intermediate Programming & Design (C++) Classes in C++

Suppose we find the following function in a file: int Abc::xyz(int z) { return 2 * z + 1; }

Written by John Bell for CS 342, Spring 2018

And Even More and More C++ Fundamentals of Computer Science

Programming, numerics and optimization

THE NAME OF THE CONSTRUCTOR AND DESTRUCTOR(HAVING (~) BEFORE ITS NAME) FUNCTION MUST BE SAME AS THE NAME OF THE CLASS IN WHICH THEY ARE DECLARED.

Object-Oriented Principles and Practice / C++

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

CSI33 Data Structures

G52CPP C++ Programming Lecture 13

Concepts (this lecture) CSC 143. Classes with Dynamically Allocated Data. List of ints w/dups (cont.) Example: List of ints with dups.

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

IS0020 Program Design and Software Tools Midterm, Fall, 2004

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

Special Member Functions

CPSC 427: Object-Oriented Programming

Copy Constructors and Assignment Operators

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

Special Member Functions. Compiler-Generated Destructor. Compiler-Generated Default Constructor. Special Member Functions

More class design with C++ Starting Savitch Chap. 11

CS32 - Week 1. Umut Oztok. June 24, Umut Oztok CS32 - Week 1

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

C11: Garbage Collection and Constructors

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

CMSC 132: Object-Oriented Programming II

Object-Oriented Programming for Scientific Computing

Classes in C++98 and C++11

CA341 - Comparative Programming Languages

More About Classes. Gaddis Ch. 14, CS 2308 :: Fall 2015 Molly O'Neil

C10: Garbage Collection and Constructors

CS558 Programming Languages

Object-Oriented Programming

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Short Notes of CS201

Major Differences between Java and C++ Programming in C++ Multiple Inheritance

An Introduction to C++

COMP 2355 Introduction to Systems Programming

Link-Based Implementations. Chapter 4

CS201 - Introduction to Programming Glossary By

C How to Program, 6/e, 7/e

ADT Sorted List Operations

CS558 Programming Languages

Fast Introduction to Object Oriented Programming and C++

CS 2604 Homework 1 Greatest Hits of C++ Summer 2000

Review: C++ Basic Concepts. Dr. Yingwu Zhu

04-17 Discussion Notes

Scope. Scope is such an important thing that we ll review what we know about scope now:

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

CS 2604 Homework 2 Solution for Greatest Hits of C++ Fall 2000

Cpt S 122 Data Structures. Course Review Midterm Exam # 2

Intermediate Programming, Spring 2017*

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

CMSC 202 Section 010x Spring Justin Martineau, Tuesday 11:30am

nptr = new int; // assigns valid address_of_int value to nptr std::cin >> n; // assigns valid int value to n

Class Vector: Interface CSE 143. Dynamic Memory In Classes [Chapter 4, p ] Vector Implementation. Many Ways to Implement. Draw the picture!

Template Issue Resolutions from the Stockholm Meeting

CS 2604 Homework 1 C++ Review Summer I 2003

Encapsulation in C++

COSC 2P95 Lab 5 Object Orientation

CSE 307: Principles of Programming Languages

CS558 Programming Languages

Midterm Review. PIC 10B Spring 2018

Chapter 13: Copy Control. Overview. Overview. Overview

Comp151. Construction & Initialization

CS24 Week 3 Lecture 1

Introduction to Programming

CS201 Some Important Definitions

EINDHOVEN UNIVERSITY OF TECHNOLOGY Department of Mathematics and Computer Science

04-19 Discussion Notes

Classes: Member functions // classes example #include <iostream> using namespace std; Objects : Reminder. Member functions: Methods.

Pointers. Developed By Ms. K.M.Sanghavi

Smart Pointers. Some slides from Internet

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Slide Set 14. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

In Java we have the keyword null, which is the value of an uninitialized reference type

Objects Managing a Resource

Object Oriented Programming COP3330 / CGS5409

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Benefits of Data Encapsulation. Internal and External Views. Lecture 4 : Data Abstraction. Data Encapsulation. Information Hiding

Vector and Free Store (Vectors and Arrays)

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Outline. User-dened types Categories. Constructors. Constructors. 4. Classes. Concrete classes. Default constructor. Default constructor

Copy Constructors & Destructors

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Pointers, Dynamic Data, and Reference Types

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

Chapter 5. Names, Bindings, and Scopes

Chapter 4: Memory. Taylor & Francis Adair Dingle All Rights Reserved

Programming in C ++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Introduction Of Classes ( OOPS )

Consider the program...

Transcription:

Deep Copy 1 Assignment of Structs 2 Slides 1. Table of Contents 2. Assignment of Structs 3. Dynamic Content 4. Shallow Copying 5. Assignment Operator 6. Deep Assignment Copy 7. Assignment Problems 8. Assignment Problems 2 9. this Pointer 10. Improved Deep Copy 11. Passing an Object 12. Passing Objects by Value 13. Passing Value Objects Results 14. Copy Constructors 15. Initialization 16. Moral A default member field assignment operation is provided for struct variables: struct EmailAcct { string ID, Host; ; void main() { EmailAcct A; A.ID = "hokie" ; A.Host = "vt.edu"; EmailAcct B; B = A; // copies the field members of A into B The default assignment operation simply copies values of the field members from the source struct into the corresponding field members of the target struct. This is satisfactory in many cases: A B However, if an struct contains a pointer to dynamically allocated memory, the result of the default assignment operation is usually not desirable

Dynamic Content 3 Shallow Copying 4 Consider the EmailAddr struct below: const int MAXALIAS = 3; struct EmailAddr { EmailAcct ; string* ; ; EmailAddr ;..ID = "hokie";..host = "vt.edu";. = new string[maxalias]; Now, suppose we declare another EmailAddr struct variable and assign the original one to it: EmailAddr Copy; Copy does not get a new copy of the array. Copy = ; It just gets a copy of the pointer from. #%&#&$! So both EmailAddr structs share the same dynamic data. Here s what results: Hmmm Copy The array is not a field member of the struct. This is almost certainly NOT what was desired when the code above was written. This is known as making a shallow copy of the source struct. There is no good solution for this problem of assigning structs that contain dynamic memory. The same problem occurs with C++ class objects, however the language provides an elegant solution.

Assignment Operator 5 Deep Assignment Copy 6 When a class object contains a pointer to dynamically allocated data, we generally will want the assignment operation to create a complete duplicate of the source object. This is known as making a deep copy. In order to do this, you must provide your own implementation of the assignment operator for the class in question: class EmailAcct { private: string ID, Host; public: EmailAcct( ); EmailAcct(string ID2, string Host2); //... Print(ostream& out); ; const int MAXALIAS = 3; class EmailAddr { private: EmailAcct ; string* ; public: EmailAddr( ); EmailAddr(string ID2, string Host2); ~EmailAddr(); EmailAddr& operator=(const EmailAddr& Eaddr); //... EmailAcct get(); string* getaliases(); ; In your own implementation of the overloaded assignment operator you must include code to handle the deep copy logic. Here s a first attempt: EmailAddr& EmailAddr ::operator=(const EmailAddr& Eaddr) { = Eaddr.; // assign non-dynamic members = NULL; // don t copy pointer = new string[maxalias]; // allocate array copy for (int i=0; i < MAXALIAS; i++) // copy array cells [i] = Eaddr.[i]; //return? The above code contains some insidious logic problems.

Assignment Problems 7 Assignment Problems 2 8 Assigning existing objects: ID: jhokie The target object may already be initialized and since the first attempt code doesn t attempt to delete its array, memory will be orphaned Second attempt: EmailAddr& EmailAddr::operator=(const EmailAddr& Eaddr) { = Eaddr.; // assign non-dynamic members delete [] ; // delete existing array = new string[maxalias]; // allocate array copy for (int i=0; i < MAXALIAS; i++) // copy array cells [i] = Eaddr.[i]; //return? Self Assignment: = ; joe joe bob jbob Deallocated Assigning: joe = ; Results in: joe bob jbob Memory Leak (garbage) joe Results in a logic error. The implicit object and Eaddr are referencing the same object. Delete deallocates the same array that is then accessed in the for loop.

this Pointer 9 Improved Deep Copy 10 Special Object Pointer: this Every object contains a language supplied implicitly defined hidden pointer to itself termed this which contains the address of the object. Used when an object needs to refer to itself as whole, not just individual data members). The this pointer is not explicitly part of the object, (i. e. not counted in the sizeof() the object). Every member function receives the this pointer as an implicit parameter It is used implicitly to access an object s members whenever a member is directly referenced. It can however be used explicitly to indirectly access an object s members. The type of the this pointer is dependent upon the type of the object to which it refers. For a non-const member function of class X, the type of the this pointer is: X * const this; // a const pointer // this is never explicitly defined or assigned For a const member function of class X, the type of the this pointer is: const X * const this; //a const pointer to a const object Here s a somewhat improved version: EmailAddr& EmailAddr::operator=(const EmailAddr& Eaddr) { if (this!= & Eaddr) { // self-assignment? = Eaddr.; // assign non-dynamic members delete [] ; // delete existing array = new string[maxalias]; // allocate array copy for (int i=0; i < MAXALIAS; i++) // copy array cells [i] = Eaddr.[i]; return( *this ); By returning a reference to an object, a member function allows chaining of the the operations. e.g., EmailAddr joe, bob; bob = joe = ; //Not the following joe.=(); bob.=(joe); Note: in the above example in all EmailAddr objects, allocated array never changes size, once allocated during execution. Thus the array would not need to be deleted and re-allocated. Its cells could be used to hold the copied strings. However, if any two objects of the EmailAddr class contained different sized arrays the above approach would need to be implemented.

Passing an Object 11 Passing Objects by Value 12 When an object is used as an actual parameter in a function call, the distinction between shallow and deep copying can cause seemingly mysterious problems. void PrintAddrs(EmailAddr mail, ostream& Out) { Out << "Email address: " << endl; mail.get().print(out); Out << "Email Aliases: " << endl; string* ealiases = mail.getaliases(); for (int i=0; i < MAXALIAS; i++) Out << ealiases[i] << endl; Note that the EmailAddr parameter mail is not passed by constant reference, but by value. However, that will cause a new problem. When an object is passed by value, the actual parameter must be copied to the formal parameter (which is a local variable in the called function). This copying is managed by using a special class constructor, called a copy constructor. By default this involves a member by member shallow copy. That means that if the actual parameter involves dynamically allocated data, then the formal parameter will share that data rather than have its own copy of it. In this case: void PrintAddrs(EmailAddr mail, ostream& Out) { Out << "Email address: " << endl; mail.get().print(out); Out << "Email Aliases: " << endl; string* ealiases = mail.getaliases(); for (int i=0; i < MAXALIAS; i++) Out << ealiases[i] << endl; Hmmm mail What happens when PrintAddrs(); is called? First, a local variable mail is created and the data members of are copied into mail, resulting in the situation shown above.

Passing Value Objects Results 13 Copy Constructors 14 When PrintAddrs() terminates, the lifetime of mail comes to an end and its destructor is automatically invoked: #%&#&$! There are several solutions to this problem: always pass objects by reference force a deep copy to be made when pass by value is used The first option is undesirable since it raises the risk of undesired modification of the actual parameter. mail Destructing mail causes the deallocation of the array to which mail. points. The second option can be achieved by providing a user-defined copy constructor for the class, and implementing a deep copy. When a user-defined copy constructor is available, it is used when an actual parameter is copied to a formal parameter. EmailAddr::EmailAddr(const EmailAddr& Source) { = Source.; // assign non-dynamic members = new string[maxalias]; // allocate array copy for (int i=0; i < MAXALIAS; i++) // copy array cells [i] = Source.[i]; But of course, that s the same array that has created. So, when execution returns to main(), will have lost its array, but. will still point to that deallocated memory, (dangling pointer). The copy constructor takes an object of the relevant type as a parameter (constant reference must be used). Implement a deep copy in the body of the copy constructor and the problem described on the previous slides is solved. Havoc will ensue.

Initialization 15 Moral 16 When an object is declared, it may be initialized with the value of an existing object (of the same type): void main3() { EmailAddr ; // default construction // code to store data into //... When implementing a class that involves dynamic allocation, if there is any chance that: objects of that type will be passed as parameters, or objects of that type will be used in initializations, or objects of that type will be returned by value EmailAddr UserEmail = ; // initialization then your implementation should include a copy constructor that provides a proper deep copy. Technically initialization is different from assignment since here we know that the target object does not yet store any defined values. Although it looks like an assignment, the initialization shown here is accomplished by the copy constructor. If there is no user-defined copy constructor, the default (shallow) copy constructor manages the initialization. If there is any chance that: objects of that type will be used in assignments then your implementation should include an overloaded assignment operator that provides a proper deep copy. This provides relatively cheap insurance against some very nasty behavior. If there is a user-defined copy constructor, it will manage the copying as the user wishes. Copy constructors also execute when an object is returned by value from a function: object x = getobject(obj);