(Redirected from Deep copy) One of the most common procedures that occurs in computer programs is the copying of data. An object is a composite data type in object-oriented programming languages. Object copy thus describes the action wherein an object has its attributes copied to another object of the same data type. An object may be copied in order to reuse all or part of its data in a new context.
The design goal of most objects is to make the programmer think that they are made out of one monolithic block even though most are not. As objects are made up of several different parts, copying becomes non trivial. Several strategies exist to attack this problem.
Consider two objects, A and B, which each refer to two memory blocks xi and yi. Think of A and B as strings and of xi and yi as the characters they contain.
The following paragraphs explain different strategies for copying A into B.
Shallow copy
One of them is the shallow copy. In this process B is attached to the same memory block as A.
This results in a situation in which some data is shared between A and B, thus modifying the one will alter the other. The original memory block of B is now no longer referred to from anywhere. If the language does not have automatic garbage collection the original memory block of B has probably been leaked.
The advantage of shallow copies is that their execution speed is fast and does not depend on the size of the data.
Bitwise copies of objects which are not made up of a monolithic block are shallow copies.
Deep copy
An alternative are deep copies. Here the data is actually copied over.
The result is different from the result a shallow copy gives. The advantage is that A and B do not depend on each other but at the cost of a slower more expensive copy.
Lazy copy
A lazy copy is a combination of both strategies above. When initially copying an object, a (fast) shallow copy is used. A counter is also used to track how many objects share the data. When the program wants to modify an object, it can determine if the data is shared (by examining the counter) and can do a deep copy if necessary.
Lazy copy looks to the outside just as a deep copy but takes advantage of the speed of a shallow copy whenever possible. The downside are rather high but constant base costs because of the counter. Also in certain situation circular references can also cause problems.
Lazy copy is related to copy-on-write.
How to copy objects
Nearly all object orientatedprogramming languages provide some way to copy objects. As most objects are not provided by the languages itself the programmer has to define how an object should be copied, just as he has to define if two objects are identical or even comparable in the first place. Many languages provide some default behavior.
How copying is solved varies from language to language and what concept of an object it has. The following presents examples for two of the most widely used object orientated languages, C++ and Java, which should cover nearly every way how an object orientated language can attack this problem.
Copying in C++
In C++ user defined objects try to behave just as builtin ones, this implies that it must be possible to construct an object based on the model of another object. A special languages construct, the copy constructor is provided to handle the problem.
class Vector{
public:
Vector();
Vector(const Vector&other):
x(other.x), y(other.y), z(other.z){
}
private:
int x, y, z;
};
Vector a;
Vector b(a); // b is a copy of a
Note that if the programmer does not provide a copy constructor the compiler will generate a default one. In certain situations it is also useful to disallow copying altogether.
Even though C++ does not enforce it at language level, it is considered as good practice to let the copy constructor do a deep copy, just as all of the built-in objects. (Pointer objects in a certain sense also do deep copies as the data pointed to is not considered to be part of the pointer.)
Copying in Java
Other than in C++ objects are only indirectly accessed through the means of references. As references are provided by the language no work from the side of the programmer is needed to make them copyable. Copying a reference is a shallow copy.
Deep copy semantics are provided by implementing the cloneable interface overriding the clone() method. Nearly all built in Java library classes correctly implement the clone method. Java's root 'Object' class does not implement the clone method, which means any user created classes do not automatically have deep copy functionality.