# Why changing variable of one object reflect in another object in below code while both are separate memory

**URL:** <https://verificationacademy.com/forums/t/why-changing-variable-of-one-object-reflect-in-another-object-in-below-code-while-both-are-separate-memory/31283>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog, new-function, deep-copy, clone\
**Created:** [December 3, 2015, 10:28pm UTC](https://verificationacademy.com/forums/t/why-changing-variable-of-one-object-reflect-in-another-object-in-below-code-while-both-are-separate-memory/31283 "2015-12-03T22:28:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JDS1](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/j/6bbea6/32.png) [@JDS1](https://verificationacademy.com/forums/u/JDS1)\
**Post date:** [December 3, 2015, 10:28pm UTC](https://verificationacademy.com/forums/t/why-changing-variable-of-one-object-reflect-in-another-object-in-below-code-while-both-are-separate-memory/31283/1 "2015-12-03T22:28:39Z")

</div>

```verilog
class base_class;
  virtual function do_copy(base_class rhs);
  endfunction
  virtual function copy(base_class rhs);
	do_copy(rhs);
  endfunction
  virtual function base_class clone();
	base_class base;
    base = new();
    base = this;
	base.copy(this);
    return (base);
  endfunction
endclass
class derived_class extends base_class;
  int A = 5;
  function do_copy(base_class rhs);
	derived_class derived;
	$cast(derived,rhs);
	super.do_copy(rhs);
	A = derived.A;
  endfunction
endclass

module test();
  derived_class d1,d2;
  initial
  begin
	d1 = new();
	d1.A = 10;
    $cast(d2,d1.clone());
	$display("A from d1 %0d",d1.A);
	$display("A from d2 %0d",d2.A);
    d1.A = 20;
	$display("A from d1 %0d",d1.A);
	$display("A from d2 %0d",d2.A);
  end
endmodule

```

Output :  
A from d1 10  
A from d2 10  
A from d1 20  
A from d2 20

===========================================================  
Here d1,d2 and d3 are separate object.  
d2 is clone of d1, which is a separate object.  
While I change d1.A to 20, why d2.A is also changed?

---

<div class="post-metadata">

**Author:** ![Van\_Hunter](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/van_hunter/32/2680_2.png) [@Van\_Hunter](https://verificationacademy.com/forums/u/Van_Hunter)\
**Post date:** [December 3, 2015, 10:55pm UTC](https://verificationacademy.com/forums/t/why-changing-variable-of-one-object-reflect-in-another-object-in-below-code-while-both-are-separate-memory/31283/2 "2015-12-03T22:55:07Z")

</div>

It rather looks like that because you initialized A in the same statement as you declared it, the compiler has taken A to be a static variable. Sure behaves like one. I would try removing the initialization (= 5) from the declaration of A. If you want all object of this class to start with a value of 5 for A, add an initial block and initialize A to that value there.

---

<div class="post-metadata">

**Author:** ![ayehia](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/ayehia/32/2099_2.png) [@ayehia](https://verificationacademy.com/forums/u/ayehia)\
**Post date:** [December 3, 2015, 11:22pm UTC](https://verificationacademy.com/forums/t/why-changing-variable-of-one-object-reflect-in-another-object-in-below-code-while-both-are-separate-memory/31283/3 "2015-12-03T23:22:42Z")

</div>

_In reply to [Jinam Shah](https://verificationacademy.com/forums/t/why-changing-variable-of-one-object-reflect-in-another-object-in-below-code-while-both-are-separate-memory/31283/2):_

Problem is in line “base = this;” in your base\_class::clone() method. This line masks the “base=new;” above it and just make the base handle points to the “d1” object. So at the end the clone does not return a new object, but rather a handle that points to the same object, i.e. both “d1” and “d2” point to the same memory location.  
To fix this your have to fix your base\_class::clone() method and add some infra structure.

```verilog

//--ADDED--
`define my_create(TYPE) \
  virtual function base_class create (); \
    TYPE t = new; \
    return t; \
  endfunction  
  
class base_class;
  //--ADDED--
  `my_create(base_class)
  virtual function do_copy(base_class rhs);
  endfunction
  function copy(base_class rhs);
	do_copy(rhs);
  endfunction
  virtual function base_class clone();
    base_class base;
    //base = new(); //--COMMENTED--
    //base = this; //--COMMENTED--
    base = this.create(); //--ADDED--
    base.copy(this);
    return (base);
  endfunction
endclass
class derived_class extends base_class;
  //--ADDED--
  `my_create(derived_class)
  int A = 5;
  function do_copy(base_class rhs);
    derived_class derived;
    $cast(derived,rhs);
    super.do_copy(rhs);
    A = derived.A;
  endfunction
endclass

```
