Systemverilog Dynamic Array

 Dynamic Array 


A dynamic array is one dimension of an unpacked array whose size can be set or changed at run-time. 
Dynamic array is Declared using a empty word subscript [ ].

The space for a dynamic array doesn’t exist until the array is explicitly created at run-time, space is allocated when new[number] is called. number indicates the number of space/elements to be allocated.

The syntax to declare a dynamic array is:

data_type  array_name [ ];
data_type is the data type of the array elements.

Dynamic array methods are,
    new[ ]       -->  allocates the storage.
    size( )       -->  returns the current size of a dynamic array.
    delete( )    -->  empties the array, resulting in a zero-sized array.

Example:
    //declaration
    bit [7:0 d_array1[ ];
    int          d_array2[ ];

    //memory allocation
    d_array1 = new[4];  //dynamic array of 4 elements
    d_array2 = new[6];  //dynamic array of  6 elements

    //array initialization
    d_array1 = {0,1,2,3};
    foreach(d_array2[j])  d_array2[j] = j;

    // change the length of the array after declaration/initialization
    d_array1 = new[10];  //dynamic array of  10 elements

In the above syntax d_array1 will get allotted with 10 new memory locations and old values of d_array1 will get deleted. old values of d_array1 elements can be retained by extending the current array by using below syntax.

    //allocate 6 new elements and retain values of 4 elements.
    d_array1 = new[10](d_array1);

    //delete array
    d_array1.delete;


 Example-1 : Dynamic Array Decalaration, Allocation and Initialization. 


module dynamic_array;
  //dynamic array declaration
  bit [7:0] d_array1[];
  int       d_array2[];

  initial begin
    $display("Before Memory Allocation");
    $display("\tSize of d_array1 %0d",d_array1.size());
    $display("\tSize of d_array2 %0d",d_array2.size());

    //memory allocation
    d_array1 = new[4]; 
    d_array2 = new[6]; 
    
    $display("After Memory Allocation");
    $display("\tSize of d_array1 %0d",d_array1.size());
    $display("\tSize of d_array2 %0d",d_array2.size());
    
    //array initialization
    d_array1 = {0,1,2,3};
    foreach(d_array2[j])  d_array2[j] = j;

    $display("--- d_array1 Values are ---");
    foreach(d_array1[i])   $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);
    $display("---------------------------------");

    $display("--- d_array2 Values are ---");
    foreach(d_array2[i])   $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
    $display("---------------------------------");
  end

endmodule

 Simulator Output  

Before Memory Allocation
Size of d_array1 0
Size of d_array2 0
After Memory Allocation
Size of d_array1 4
Size of d_array2 6
--- d_array1 Values are ---
d_aaray1[0] = 0
d_aaray1[1] = 1
d_aaray1[2] = 2
d_aaray1[3] = 3
---------------------------------
--- d_array2 Values are ---
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
d_aaray2[3] = 3
d_aaray2[4] = 4
d_aaray2[5] = 5
---------------------------------
Execute the above code on 

 Example-2 : Dynamic Array, delete() method. 


module dynamic_array;
  
  //dynamic array declaration
  bit [7:0] d_array1[];
  int       d_array2[];

  initial begin
    //memory allocation
    d_array1 = new[2]; 
    d_array2 = new[3]; 
        
    //array initialization
    d_array1 = {2,3};
    foreach(d_array2[j])  d_array2[j] = j;

    $display("--- d_array1 Values are ---");
    foreach(d_array1[i])   $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);
    $display("---------------------------------");

    $display("--- d_array2 Values are ---");
    foreach(d_array2[i])   $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
    $display("---------------------------------");
    
    //delete array
    d_array1.delete;
    d_array2.delete;
    
    $display("After Array Delete");
    $display("\tSize of d_array1 %0d",d_array1.size());
    $display("\tSize of d_array2 %0d",d_array2.size());
  end
endmodule

 Simulator Output  


--- d_array1 Values are ---
d_aaray1[0] = 2
d_aaray1[1] = 3
---------------------------------
--- d_array2 Values are ---
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
---------------------------------
After Array Delete
Size of d_array1 0
Size of d_array2 0
Execute the above code on 

 Example-3 : Dynamic Array, Increasing array size by overriding and retaining old values. 


module dynamic_array;
  
  //dynamic array declaration
  bit [7:0] d_array1[];
  int       d_array2[];

  initial begin
    //memory allocation
    d_array1 = new[2]; 
    d_array2 = new[3]; 
        
    //array initialization
    d_array1 = {2,3};
    foreach(d_array2[j])  d_array2[j] = j;

    $display("----- d_array1 Values are -----");
    foreach(d_array1[i])   $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);

    $display("----- d_array2 Values are -----");
    foreach(d_array2[i])   $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
    
    //Increasing the size by overriding the old values 
    d_array1 = new[4]; //Create dynamic array of 4 elements
    $display("Size of Array d_array1 %0d",d_array1.size());
    $display("----- d_array1 Values are -----");
    foreach(d_array1[i])   $display("\td_aaray1[%0d] = %0d",i, d_array1[i]);

    //Increasing the size by retaining the old values 
    d_array2 = new[5](d_array2); //Create dynamic array of 5 elements, retaining old values 
    $display("Size of Array d_array2 %0d",d_array2.size());
    $display("----- d_array2 Values are -----");
    foreach(d_array2[i])   $display("\td_aaray2[%0d] = %0d",i, d_array2[i]);
    
  end
endmodule

 Simulator Output  


----- d_array1 Values are -----
d_aaray1[0] = 2
d_aaray1[1] = 3
----- d_array2 Values are -----
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
Size of Array d_array1 4
----- d_array1 Values are -----
d_aaray1[0] = 0
d_aaray1[1] = 0
d_aaray1[2] = 0
d_aaray1[3] = 0
Size of Array d_array2 5
----- d_array2 Values are -----
d_aaray2[0] = 0
d_aaray2[1] = 1
d_aaray2[2] = 2
d_aaray2[3] = 0
d_aaray2[4] = 0
Execute the above code on