Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deep copy vs Shallow Copy [duplicate]

Tags:

c++

clone

Possible Duplicate:
What is the difference between a deep copy and a shallow copy?

What is the difference between deep and shallow copy. What type of a copy does a copy constructor do?

like image 597
Ankur Avatar asked Apr 17 '10 08:04

Ankur


People also ask

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

In Shallow copy, a copy of the original object is stored and only the reference address is finally copied. In Deep copy, the copy of the original object and the repetitive copies both are stored.

What is the shallow copy?

A shallow copy of an object is a copy whose properties share the same references (point to the same underlying values) as those of the source object from which the copy was made.

What is difference between shallow copy and deep copy in IOS?

A deep copy duplicates the objects referenced while a shallow copy duplicates only the references to those objects. So if object A is shallow-copied to object B, object B refers to the same instance variable (or property) that object A refers to.

When would you use a shallow copy?

A shallow copy creates a new object which stores the reference of the original elements. So, a shallow copy doesn't create a copy of nested objects, instead it just copies the reference of nested objects. This means, a copy process does not recurse or create copies of nested objects itself.


2 Answers

Shallow copy:

Some members of the copy may reference the same objects as the original:

class X { private:     int i;     int *pi; public:     X()         : pi(new int)     { }     X(const X& copy)   // <-- copy ctor         : i(copy.i), pi(copy.pi)     { } }; 

Here, the pi member of the original and copied X object will both point to the same int.


Deep copy:

All members of the original are cloned (recursively, if necessary). There are no shared objects:

class X { private:     int i;     int *pi; public:     X()         : pi(new int)     { }     X(const X& copy)   // <-- copy ctor         : i(copy.i), pi(new int(*copy.pi))  // <-- note this line in particular!     { } }; 

Here, the pi member of the original and copied X object will point to different int objects, but both of these have the same value.


The default copy constructor (which is automatically provided if you don't provide one yourself) creates only shallow copies.

Correction: Several comments below have correctly pointed out that it is wrong to say that the default copy constructor always performs a shallow copy (or a deep copy, for that matter). Whether a type's copy constructor creates a shallow copy, or deep copy, or something in-between the two, depends on the combination of each member's copy behaviour; a member's type's copy constructor can be made to do whatever it wants, after all.

Here's what section 12.8, paragraph 8 of the 1998 C++ standard says about the above code examples:

The implicitly defined copy constructor for class X performs a memberwise copy of its subobjects. [...] Each subobject is copied in the manner appropriate to its type: [...] [I]f the subobject is of scalar type, the builtin assignment operator is used.

like image 77
stakx - no longer contributing Avatar answered Oct 01 '22 12:10

stakx - no longer contributing


The quintessential example of this is an array of pointers to structs or objects (that are mutable).

A shallow copy copies the array and maintains references to the original objects.

A deep copy will copy (clone) the objects too so they bear no relation to the original. Implicit in this is that the object themselves are deep copied. This is where it gets hard because there's no real way to know if something was deep copied or not.

The copy constructor is used to initilize the new object with the previously created object of the same class. By default compiler wrote a shallow copy. Shallow copy works fine when dynamic memory allocation is not involved because when dynamic memory allocation is involved then both objects will points towards the same memory location in a heap, Therefore to remove this problem we wrote deep copy so both objects have their own copy of attributes in a memory.

In order to read the details with complete examples and explanations you could see the article Constructors and destructors.

The default copy constructor is shallow. You can make your own copy constructors deep or shallow, as appropriate. See C++ Notes: OOP: Copy Constructors.

like image 22
cletus Avatar answered Oct 01 '22 13:10

cletus