Array of Class handles "No implication of declaring a limit on no of handles?"

Hi,

Consider the piece of code below.
Please follow the comments in the code.


class top;
  int a=1;
  function new(int i=0);
    $display("Class Handle %0d, A= %0d",i,this.a);
  endfunction
endclass
module test;
 top top1[2];  // No Implication of limit specified here I can create more handles than 2. 
 initial
   begin
    top1[0] = new(0);  // legal
    top1[1] = new(1);  // legal
    top1[2] = new(2);  //Overriding Behavior not as expected
    top1[3] = new(3);  //Overriding Behavior not as expected
    top1[4] = new(4);  //Overriding Behavior not as expected
   end
endmodule

Simulation will create 5 class objects, as compiler fails to restrict to 2 handles declared.
Then what is the Implication of declaring 2 handles in first place?

Thanks
Parveez

Like in most programming languages, there is no range checking of array indexes. However, SystemVerilog ignores the LHS and there will be no assignment. In C it would be easy to corrupt another variable not in the array. Regardless, an object is created when calling the constructor as it evaluates the RHS, but will promptly be removed because no reference to that object exists.

What is happening in your testcase is actually slightly different than you describe.

IEEE 1800-2012 74.6 says:

Writing to an array with an invalid index shall perform no operation, with the exceptions of
writing to element [$+1] of a queue (described in 7.10.1) and creating a new element of an associative array
(described in 7.8.6). Implementations may issue a warning if an invalid index occurs for a read or write
operation on an array.

The key phrase in that is “Implementations may issue a warning…”

This means each simulation vendor is free to choose for themselves if they issue warning message in cases like this or not. For example in QuestaSim we do not issue a warning in default mode, but you will get the warning

** Warning: class_array.sv(13): (vopt-2696) Element index 1 into ‘top1’ is out of bounds.

if you compile in -lint mode.

If you add a $display statement that prints the contents or size of your ‘top1’ array, you will see that it actually only contains 2 elements and the end your initial block

In reply to alexgran:

Yes, But i did some more experiments in the code to check on null objects for top[2] & top[3].
please follow the code and comments below.

original code was modified by adding a function ‘b’ to class “top”
Displaying Class objects in memory.
Call the function in the last portion of initial block.


class top;
  int a=1;
  function new(int i=0);
    $display("Class Handle %0d, A= %0d",i,this.a);
  endfunction

  //Added a function used later to see even the null objects can have this function definition which can be called
  function int b(int i=0);
    $display("Class Handle %0d,Calling function B ",i);
    return(i);
  endfunction
endclass

module test;
 top top1[2];
 initial
   begin
    top1[0] = new(0);
    top1[1] = new(1);
    top1[2] = new(2);
    top1[3] = new(3);

   //Displaying Class Objects created in foreach loop to see how many class objects exist
    foreach(top1[i])
      $display(top1[i]);  //will display 2 objects, probably for top[0] & top[1]
  
   //Displaying Class Objects created by manually specifying indexes
    $display(top1[0]);   //will display object of top[0]
    $display(top1[1]);   //will display object of top[1]
    $display(top1[2]);   // will show null @ simulation
    $display(top1[3]);   // will show null @ simulation
  
   //function B existing in each of the class can be called though! (both null and the existing)
    $display(top1[0].b(0)); 
    $display(top1[1].b(1));
    $display(top1[2].b(2));
    $display(top1[3].b(3));
  
   end
endmodule

Summary

The default foreach will only display only 2 class handles as you described.
By manually writing the indexes and displaying the class objects will show them as null.
But there exists pointers to function ‘b’ in each of the class objects?

Thanks
Parveez

In reply to Parveez ahamed:

All class methods are defined with an implicit this handle argument. So your method b is really

function int top::b(top this, int i=0);
    $display("Class Handle %0d,Calling function B ",i);
    return(i);
  endfunction

and any call to a method is replaced with

$display(top::b(top1[2],2));

It happens that your method b does not refer to any class properties, so the this handle is never referenced. If instead you wrote you method b to display a

function int b();
    $display("Class Handle %0d,Calling function B ",a);
    return(i);
  endfunction

it would get converted to

function int top::b(top this);
    $display("Class Handle %0d,Calling function B ",this.a);
    return(i);
  endfunction

A null this handle would produce an error.

In reply to dave_59:

Yes,
I have modified my above program to below by adding display to class member,

\-------------------------
function int b(int i=0);
$display(this.a); //------------------------ This will give me a fatal : Bad handle reference
$display("Class Handle %0d,Calling function B ",i);
return(i);
endfunction
\-------------------------

Thanks
Parveez