views:

7893

answers:

8

What is the difference between a deep copy and a shallow copy?

+22  A: 

In short, it depends on what points to what. In a shallow copy, object B points to object A's location in memory. In deep copy, all things in object A's memory location get copied to object B's memory location.

This wiki article has a great diagram.

http://en.wikipedia.org/wiki/Object_copy

contagious
+8  A: 

Shallow copy: Copies the member values from one object into another.

Deep Copy:    Copies the member values from one object into another.
                     Any pointer objects are duplicated and Deep Copied.

Example:

class String
{
     int   size;
     char* data;
};

String  s1("Ace");   // s1.size = 3 s1.data=0x0000F000

String  s2 = shallowCopy(s1);
 // s2.size =3 s2.data = 0X0000F000
String  s3 = deepCopy(s1);
 // s3.size =3 s3.data = 0x0000F00F
 //                      (With Ace copied to this location.)
Martin York
+20  A: 

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.

Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.

S.Lott
+1  A: 
char * Source = "Hello, world.";

char * ShallowCopy = Source; 

char * DeepCopy = new char(strlen(Source)+1);
strcpy(DeepCopy,Source);

'ShallowCopy' points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the same.

John Dibling
+1  A: 
var source = { firstName="Jane", lastname="Jones" };
var shallow = ShallowCopyOf(source);
var deep = DeepCopyOf(source);
source.lastName = "Smith";
WriteLine(source.lastName); // prints Smith
WriteLine(shallow.lastName); // prints Smith
WriteLine(deep.lastName); // prints Jones
Dour High Arch
That's not a good example. Shallow copies are mostly used for quick copying of objects, without copying the data, but once an objects needs to modify the shared data, a deep copy of it is taken. Your example will likely confuse beginners.
iconiK
+7  A: 

I haven't seen a short, easy to understand answer here--so I'll give it a try.

With a shallow copy, any object pointed to by the source is pointed to by the destination (so that no referenced objects are copied).

With a deep copy, any object pointed to by the source is copied into the destination (so there will now be 2 of each referenced object).

Bill K
+7  A: 
dlamblin
+1 for short and nice illustration.
penguru
+4  A: 

In object oriented programming, a type includes a collection of member fields. These fields may be stored either by value or by reference (i.e., a pointer to a value).

In a shallow copy, a new instance of the type is created and the values are copied into the new instance. The reference pointers are also copied just like the values. Therefore, the references are pointing to the original objects. Any changes to the members that are stored by reference appear in both the original and the copy, since no copy was made of the referenced object.

In a deep copy, the fields that are stored by value are copied as before, but the pointers to objects stored by reference are not copied. Instead, a deep copy is made of the referenced object, and a pointer to the new object is stored. Any changes that are made to those referenced objects will not affect other copies of the object.

Jeffrey L Whitledge