DOC PREVIEW
UMD CMSC 131 - Lecture 19: Issues in Copying

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 19 Issues in Copying Last time 1 Intro to arrays 2 Copying arrays and making arrays bigger 3 Array lengths and out of bounds indexing 4 Passing arrays and array elements to a function This lecture set 1 Privacy Leaks 2 Different levels of copy CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr Privacy Leaks continued public class MutableThing public void mutateMe public class Foo private MutableThing q new MutableThing public MutableThing getQ return q Consider following code Foo f new Foo MutableThing m f getQ m mutateMe After this executes what happens This phenomenon is called a privacy leak Stack Heap f m Foo object MutableThing object Private instance variables can be modified outside class Behavior is due to aliasing CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 1 Fixing Privacy Leaks Return copies of objects referenced by instance variables To fix getQ method in Foo Stack f m MutableThing getQ return new MutableThing q Heap This returns a copy of q Changes made to this copy will not affect original CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr MutableThing object Foo object MutableThing object 2 Reference Copying Person d new Person SGH new Person Shakira Stack Heap d e Person e d SGH Shakira CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 3 Shallow Copying Person d new Person SGH new Person Shakira Stack Heap d e Person e new Person d length for int i 0 i d length i e i d i SGH Shakira CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 4 Deep Copying Person d new Person SGH new Person Shakira Person e new Person d length for int i 0 i d length i e i new Person d i Stack Heap d e SGH Shakira SGH Shakira CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 5 Three Ways of Copying CDCollector contains an array of CD s ReCDCollector contains an array of rewritableCD s Reference copy public ReCD getCDsReferenceCopy return myFavorites Shallow copy public ReCD getCDsShallowCopy ReCD copy new ReCD myFavorites length for int i 0 i copy length i copy i myFavorites i return copy ReCDCollectionOwner p new RECD ReCD a p getCD a 0 otherCDalreadycreated a 0 rewrite other name Deep copy public ReCD getCDsDeepCopy ReCD copy new ReCD myFavorites length for int i 0 i copy length i copy i new ReCD myFavorites i return copy CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 6 When To Use What Kind of Copying Reference copying is usually a bad idea not always but realize what you are doing Deep copying provides maximal protection against aliasing but takes a lot of time and space if it was not necessary Storage space and time used Reference least Shallow middle Deep most If the class is mutable aliasing is something to be avoided and you must have true copies to prevent privacy leaks and modifications outside If you know the class is immutable aliasing doesn t hurt but neither does making true copies except wasted space and time If storage is an issue aliasing problems may be worth coping with but must be well documented CMSC 131 Fall 2007 Jan Plane adapted from Bonnie Dorr 7


View Full Document

UMD CMSC 131 - Lecture 19: Issues in Copying

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 19: Issues in Copying
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 19: Issues in Copying and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 19: Issues in Copying and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?