System Verilog Queues

hi

I have doubt in following example related to queues. In this scenario, same transaction class randomized twice and pushed each randomization into two different queues (q1 and q2) of “c” (transaction) data type,respectively. When I popping from both queues, I am getting same address instead different addresses. I am bit confused on this, can anyone please clarify on this.

class c;
  rand logic [3:0] addr;
endclass:c

module m;
c c1;
c txn1,txn2;
c q1[$],q2[$];

initial
  begin
    c1 = new();

    //**** First Randomization *****
    c1.randomize();
    $display("First addr=%h",c1.addr);        //addr = 4'hB
    q1.push_back(c1);

    //**** Second Randomization *****
    c1.randomize();
    $display("Second addr=%h ",c1.addr);      //addr = 4'h3
    q2.push_back(c1);

    txn1 = q1.pop_front(); 
    $display("queue1 addr=%h",txn1.addr);     //Getting addr = 4'h3, **instead of addr = 4'hB**
    txn2 = q2.pop_front(); 
    $display("queue2 addr=%h ",txn2.addr);    //addr = 4'h3
  end

Thanks in advance,
Jagan Kudimi

In reply to jagan413:

This will solve the problem,


c1 = new();

//**** First Randomization *****
c1.randomize();
$display("First addr=%h",c1.addr); //addr = 4'hB
q1.push_back(c1);

// NOTE: You need to create 1 more object on handle c1
// Second time, a new object will be randomized instead
// of the same object as in your code.
c1 = new();

//**** Second Randomization *****
c1.randomize();
$display("Second addr=%h ",c1.addr); //addr = 4'h3
q2.push_back(c1);

When you randomize and push the object in queue, handle c1 as well as q1[0] both will point to a single object.
And 2nd time also randomization will be done on same object and same object is being pushed onto the other queue.
So at the end, handle c1, q1[0] and q2[0] all points to same object only. Thats the reason you are getting same values from both the queue.

Hint: Visualize flow of objects when doing handle assignments.