dynamic arrays:

A dynamic array is one dimension of an unpacked array whose size can be set or changed at run-time.

 Array size is provided at run-time and not compile-time
 Can be allocated and resized during simulation
 Declared with empty subscripts [   ]
 new[  ] operator is used to allocate space, passing the number of entries
 Can also pass name of an array to copy values
 $size function returns the size of a fixed-size or dynamic array
Can use several built in routines such as delete and size
 You can make assignments between fixed arrays and dynamic arrays as
long as they have the same:
► Base type
► And same number of elements


Syntax for dynamic arrays

data_type name_of_dynamic_array[  ];
name_of_dynamic_array=new[number of elements];

Resize of dynamic arrays

In dynamic arrays also resize its current size during the run time using constructor new[]; without loosing exiting data content data_type name_of_dynamic_array[  ];
name_of_dynamic_array=new[number of elements];

Examples:

int dynomic_array1 [ 10 ];
dynomic_array1 = new [20]dynomic_array1;

Example 01:

module dyn_array();

int dynamic_array[];

initial begin
dynamic_array = new[100];
dynamic_array[5] = 96;
$display(dynamic_array[5]);
$display(dynamic_array[4]);
$display(dynamic_array.size());
dynamic_array.delete();
$display(dynamic_array.size());
end

endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# 96
# 0
# 100
# 0
# ------------------------------------------------------

Example 02:

//Creation,resizing and deletion of dynamic array
module dyanamicarray;
int a[],b[];
int c[]; //='{1,2,3};
initial begin
a = new [10]; //create a dynamic array of 10 elements
foreach (a[i])
a[i] = i+10; //initialising array
b = a;//copying a to b
b[0]=49;//changing the first value of b
c = '{11,12,3};
$display(b);
$display (a);
a= new[20](a);//resize the array while copying the existing elements
$display (a);
a = new[100];//deallocates all existing values of a & creates 100 spaces
$display (a);
a.delete();//deletes the memory space
b.delete();
$display(b);
$display(c.size());
end
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# 49 11 12 13 14 15 16 17 18 19
# 10 11 12 13 14 15 16 17 18 19
# 10 11 12 13 14 15 16 17 18 19 0 0 0 0 0 0 0 0 0 0
# 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 #
# 3
# ------------------------------------------------------

Example 02:

module resize_darr;
int darr_1[];

initial
begin
darr_1 = new[4]('{1,2,3,4});
foreach(darr_1[i])
begin
$display("%d index = %d", i, darr_1[i]);
end
darr_1 = new[8](darr_1);
foreach(darr_1[i])
begin
$display("%d index = %d", i, darr_1[i]);
end
end
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# 0 index = 1
# 1 index = 2
# 2 index = 3
# 3 index = 4
# 0 index = 1
# 1 index = 2
# 2 index = 3
# 3 index = 4
# 4 index = 0
# 5 index = 0
# 6 index = 0
# 7 index = 0
# ------------------------------------------------------

Example 02:

module array_basics;
initial
begin
//declaring and initialising a 1d array

int a[4]='{0,1,2,3};
integer b[7];
//declaring and initialising a 2d array
int c[4][4]='{'{0,2,3,4},'{1,2,2,1},'{5,5,4,7},'{4,5,7,2}};
$display ("A0=%d,A1=%d,A2=%d,A3=%d",a[0],a[1],a[2],a[3]);
a[0]=2;//Replacing the first location of the array
$display ("A0=%d,A1=%d,A2=%d,A3=%d",a[0],a[1],a[2],a[3]);
b[0:3]='{4{7}};
$display ("b=%p",b);//%p is the access specifier for printing the whole array
a = '{default:10}; //all indices of an array can be written a default value
$display ("A0=%d,A1=%d,A2=%d,A3=%d",a[0],a[1],a[2],a[3]);
foreach (a[i])//it iterates automatically without any need of explicitly incrementing the variable
a[i] *= a[i];
$display ("a=%p",a);
//displaying multidimensional array using foreach
foreach(c[i,j])
$display("c[%d][%d]=%d",i,j,c[i][j]);
//displaying Multiple Dimension array in matrix form using foreach(similar to nested for loop in other programming languages)

foreach(c[i])begin
$write("%2d:",i);
foreach(c[,j])
$write("%3d",c[i][j]);
$display;
end
end
//$display and $write perform the same function of printing out values on console. $display adds a “\n” character after the end of the line
endmodule

OUT PUT :

# ----------------OUTPUT RESULT -----------------------
# A0= 0,A1= 1,A2= 2,A3= 3
# A0= 2,A1= 1,A2= 2,A3= 3
# b='{7, 7, 7, 7, x, x, x} # A0= 10,A1= 10,A2= 10,A3= 10
# a='{100, 100, 100, 100}
# c[ 0][ 0]= 0
# c[ 0][ 1]= 2
# c[ 0][ 2]= 3
# c[ 0][ 3]= 4
# c[ 1][ 0]= 1
# c[ 1][ 1]= 2
# c[ 1][ 2]= 2
# c[ 1][ 3]= 1
# c[ 2][ 0]= 5
# c[ 2][ 1]= 5
# c[ 2][ 2]= 4
# c[ 2][ 3]= 7
# c[ 3][ 0]= 4
# c[ 3][ 1]= 5
# c[ 3][ 2]= 7
# c[ 3][ 3]= 2
# 0: 0 2 3 4
# 1: 1 2 2 1
# 2: 5 5 4 7
# 3: 4 5 7 2
# ------------------------------------------------------