Qualcomm Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Phone Interview
Also known as address copy as in this process B is attached to same memory block as A (for copying A to B). This results in situation where same data is being shared between A and B thus modifying the one will alter the other. Advantage - Execution speed is fast and does not depend on data size.
Shallow copy: When the data in one data structure (usually class instance) is copied and created a new instance BUT only the top-level data is copied (e.g. the data belonging to the embedded references are not)
- Selmeczy, Péter November 05, 2012Deep copy: Object is copied in a way that all data in the references it contains (and all inside those references, etc. recursively) is copied.
E.g. if there is a class that contains 3 points (they are themselves class-instances) and a colour (not a class, just a simple enum) a shallow copy copies the referene to the 3 points and the colour creating a new instance of that class. However using this object and modifying the points data - it will modify the data in the original copy as well (well, the data that is referenced to) In this case only one new object created.
With a deep copy all three point-instances are copied by creating new instances, too, so making any change using this new object has no impact to the old object whatsoever. In this case 1+3 new objects are created.