Shallow vs Deep Copy (with examples)
- A shallow copy shares its children with the original
- A deep copy has its own copies of the children
The above example showed an object graph. Same story for arrays.
If there are no children?
Since the concept of deep vs shallow concerns treatment of children, it’s meaningless to talk about deep vs shallow copying of objects (or arrays) that doesn’t have any children. When creating a copy of an array of integers for example, the copy can be seen as both shallow (no children were copied) and deep (all children were copied).
pointerA = pointerB?
When you do
pointerA = pointerB no object is copied, so it’s neither a deep nor a shallow copy. A pointer value is copied but the value itself (the address) can’t possibly have any children so it doesn’t make sense to try to categorize this copying as shallow or deep.
Mixed copies are part shallow, part deep. If you for instance make shallow copies of the immediate children, then the original and the copy may share the same grand children. You could say that the copying is one level deep.
Another type of mixed copy would be if you create copies of some children, while sharing others. If you for example have a reference to a singleton, or an immutable object it doesn’t make sense to clone these objects.
When to use which?
This has to be decided on a case by case basis. Here are a few pro’s and con’s of each approach to help you decide.
Shallow copies are easier to implement since they don’t require traversal of the object tree.
If the data structure to be copied can have cycles, a deep copy implementation needs to keep track of already copied objects to avoid infinite copying.
If the data structure to be copied is immutable, there’s no risk in sharing state and shallow copies are therefore typically preferred.
If the data structure is mutable, sharing data could lead to complex code. For this reason deep copies may be preferred. There’s also a middle way, where shallow copies are used until the children are modified, at which point they are copied. This “lazy deep copying” is called copy on write.