Shallow copy Vs Deep copy

An object copy is a process where a data object has its attributes copied to another object of the same data type.

Shallow copy is also known as Address copy. In this process you only copy address not actual data, while in Deep copy you copy data also.

Fig of Shallow copy:

shallow-copy

Explanation of Shallow Copy:

Shallow copying is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. So a shallow copy of an object is a new object whose instance variables are identical to the old object.

Where to use Shallow copy:

The situations like , if you have an object with values and you want to create a copy of that object in another variable from same type, then you can use shallow copy. All property values which are of value types will be copied, but if you have a property which is of reference type then this instance will not be copied, instead you will have a reference to that instance only.

What is Deep copy ?

deep-copy

Deep copy is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type, a bit by bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed.

                                     A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old.

Deep copy is intended to copy all the elements of an object, which include directly referenced elements of value type and the indirectly referenced elements of a reference type that holds a reference to a memory location that contains data rather than containing the data itself.

Difference between Static and Non-Static fields of a class.

Static variables or fields belong to the class, and not to any object of the class. A static variable is initialized when the class is loaded at runtime. Non-static fields are instance fields of an object. They can only be accessed or invoked through an object reference. The value of static variable remains constant throughout the class. The value of Non-static variables changes as the objects has their own copy of these variables.

More explanation of Shallow copy: Suppose there are two objects A and B. A is pointing to a different array while B is pointing to different array.

Char *A = {‘a’,’b’,’c’};
Char *B = {‘x’,’y’,’z’};
B = A;

Shallow copy process -> Now B is pointing is at same location where A pointer is pointing.Both A and B in this case sharing the same data. If change is made(in A or B), data in other will get altered value of data.Advantage is that coping process is very fast and is independent of size of array.

Deep copy process ->While in Deep copy, data is also copied. This process is slow but Both A and B have their own copies and changes made to any copy, other will copy will not be affected.

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. i.e

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).

Fig:

Shallow:

Before Copy Shallow Copying Shallow Done

The variables A and B refer to different areas of memory, when B is assigned to A the two variables refer to the same area of memory. Later modifications to the contents of either are instantly reflected in the contents of other, as they share contents.

Deep:

Before Copy Deep Copying Deep Done

The variables A and B refer to different areas of memory, when B is assigned to A the values in the memory area which A points to are copied into the the memory area to which B points. Later modifications to the contents of either remain unique to A or B; the contents are not shared.

In Simple Terms, a Shallow Copy is similar to Call By Reference and a Deep Copy is similar to Call By Value

In Call By Reference, Both formal and actual parameters of a function refers to same memory location and the value.

In Call By Value, Both formal and actual parameters of a functions refers to different memory location but having the same value.

Especially For iOS Developers:

If B is a shallow copy of A, then for primitive data it’s like B = [A assign]; and for objects it’s like B = [A retain];

B and A point to the same memory location

If B is a deep copy of A, then it is like B = [A copy];

B and A point to different memory locations

B has same contents as A’s

Try to consider following image

enter image description here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s