Function call; fields of the object have junk value

Hello,

I have the following code:


class Packet extends uvm_object;
  `uvm_object_utils(Packet)
  bit[7:0] opcode;
endclass



module TB;

 

  function void B();
     Packet temp_pkt;
     temp_pkt = new();
     temp_pkt.opcode = 'h2;
     A(temp_pkt);
  endfunction
  
   function void A(Packet pkt);
     pkt = new();
     $display(“Opcode value is %b \n”, pkt.opcode);//junk is printed here; why? 
  endfunction

  
  
  function void run();
    B();
  endfunction  

  initial begin
    run();
  end
  
  
endmodule
   

Any idea why I am not able to print opcode?

Thanks!

In reply to Verif Engg:

Because you are creating a new pkt object with “pkt = new();” in the function A(), before $display.

In reply to S.P.Rajkumar.V:

Thanks.

The same problem occurs even if I remove “pkt = new();” :(

Not sure what I am missing

Definition of new() is missing in your Packet class.
Add the following:
function new(string name = “Packet”);
super.new(name);
endfunction : new

and also you need to remove “pkt=new();” in function A();

In reply to Verif Engg:

Check here. Opcode of 'h2 is displayed as output after commenting pkt=new(). What are you really expecting?

In reply to S.P.Rajkumar.V:

In reply to Verif Engg:
Check here. Opcode of 'h2 is displayed as output after commenting pkt=new(). What are you really expecting?

Thanks ! Appreciate the link