Simple queue of classes works on a NULL handle?

Hello,

The code is working as expected but am wondering how are we able to access without creating class instances for the queue of widgets as we are operating on NULL handles in it’s current form.

I expected this to error out when compiled but it works. Please clarify. If we are pushing anything to the queue which is actually a class, it can’t be valid as there is no memory allocated.


class widget;
  int id;
  bit to_remove;
endclass : widget

module top;
  widget q[$];
  widget temp[$];
  initial begin
    widget w;
    int num = $urandom_range(20,40);
    for (int i = 0; i < num; i++) begin
      w = new;
      w.id = i;
      w.to_remove = $urandom_range(0,1);
      q.push_back(w);
      $display("widget id:%02d, to_remove:%b", q[$].id, q[$].to_remove);
    end
    
    for (int i=0;i<num;i++) begin
      w=q[i];
      if(w.to_remove==0)
        temp.push_back(w);
    end
  
    // write SV code to remove entries in q[$] that have to_remove==1
    $display("\n\n-------------------------------------");
    
    foreach(temp[i])
      begin
       $display("widget id:%02d, to_remove:%b  --> OK", temp[i].id,temp[i].to_remove);
      end
  end
endmodule


In reply to hsam:

I don’t understand where you think you are accessing null handles. You are copying class handles onto the queue, not class objects.

In reply to dave_59:

In reply to hsam:
I don’t understand where you think you are accessing null handles. You are copying class handles onto the queue, not class objects.

q.push_back(w) is equivalent to the example below in the way the handles are passed w.r.t my original code?


class a=new();
class b;
b = a;//copy b handle to a so now both b, a point to the same object

In reply to hsam:

Correct. Queue temp has a subset of handles that q has.

In reply to dave_59:

Thank you for confirming. I missed a simple one in retrospection :)