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