Thursday, November 5, 2015

Shallow Copy Deep Copy

Shallow copy : As name suggest, it is copy of class object with little depth. How? I will explain below.

There are 3 ways, one class property is assigned to another class variable (of course of same type).

1st  : (Assignment)
class A;
  int j = 10;
endclass
class check_copy;
 int i = 1;
 A a;
 function new();
   a = new();
 endfunction
endclass
module test();
  check_copy p1,p2;
  initial
  begin
    p1 = new();
    p2 = p1;
    p1.i = 2;
    p1.a.j = 11;
    $display(p2.i,p2.a.j);
  end
endmodule

Output :
 2         11

Run Here:

Here p1 and p2 points to same object, share same memory location. There are two names (p1 & p2) to access that object


2nd : Shallow Copy

class A;
  int j = 10;
endclass
class check_copy;
 int i = 1;
 A a;
 function new();
   a = new();
 endfunction
endclass
module test();
  check_copy p1,p2;
  initial
  begin
    p1 = new();
    p2 = new p1;
    $display(p2.i,p2.a.j);
    p1.i = 2;
    p1.a.j = 11;
    $display(p2.i,p2.a.j);
  end
endmodule
Output :
 1         10
 1         11
Run Here:
Here new object for p2 is created and values from p1 are copied to p2. But see here when we 
change p1.i, it do not change p2.i, both are separate memory. But when we change p1.a.j it is 
reflected in p2.a.j, both are shared memory. "a" has been new only once.
p2 = new p1; has encountered something like below.
 
p2 = new();
p2.i = p1.i;
p2.a = p1.a;
See here p2.a = p1.a works as descried in "1st way" (see above)
3rd : deep copy
Here we copy everything in a separate copy task, which create new for the nested object.
class A;
  int j = 10;
endclass
class check_copy;
 int i = 1;
 A a;
 function new();
   a = new();
 endfunction
 function copy (check_copy p);
   this.i = p.i;
   this.a = new p.a;
 endfunction
endclass
module test();
  check_copy p1,p2;
  initial
  begin
    p1 = new();
    p2 = new ;
    p2.copy(p1);
    $display(p2.i,p2.a.j);
    p1.i = 2;
    p1.a.j = 11;
    $display(p2.i,p2.a.j);
  end
endmodule
Output :
 1         10
 1         10
Run Here:
Here for p2.a, a separate object has been created. We need to create separate object for every 
nested class in copy function. That will be deep copy (values will be copied initially, but 
change into one object will not reflect in second.)  
Conclusion :
A deep copy is one where the value of each of the individual properties in a data object 
are copied to another, as opposed to a shallow copy where just the data pointer is copied.
Question :

Is Shallow copy p2 = new p1; is same as p2 = new ; p2 = p1; ?

No. Why?
Because  new p1; is copy constructor (Not just new constructor). That construct one object and place their handle to p2 as well copy all variable of p1 to p2. But there will be two different object, whose handle will be stored in p1 and p2. So change in one object would not reflect in second. 

3 comments:

  1. Thank you so much it helped me a lot m struggling from a long time to understand I understood it very well

    ReplyDelete
  2. hi, i have one doubt. as you written,
    "Shallow copy p2 = new p1; is same as p2 = new ; p2 = p1; ?"
    in case p1=new, p2=new, p2=p1; variables are copied and object handle is copied. so any change in p1.a will reflect in p2.a?? right.

    but in code, p2=new p1; same thing is again happening in your code but in your conclusion , you are writing
    "Because new p1; is copy constructor (Not just new constructor). That construct one object and place their handle to p2 as well copy all variable of p1 to p2. But there will be two different object, whose handle will be stored in p1 and p2. So change in one object would not reflect in second."

    change in one object will not reflect in second.
    please clear it, i am bit confuse.

    ReplyDelete
    Replies
    1. That is what, when you do p2=new p1. It essentially create a new handle p2 for a new object. And p2 gets copy of all its member from p1. (p2.i = p1.i , p2.a=p1.a). Now here class handle "a" reference to same object, either you call it p1.a or p2.a.
      Hence any change in variable of j in one class handle will be visible to other class handle. Hence they call it shallow(without depth) copy. Essentially same object, change in one would reflect in other.

      Delete