Hi,
in the code below I have declaring the dynamic array of size 3 and I am assigning values more than declared size but instead of truncating the extra values it was printing all the values that I was assigning. Even if I comment the da=new[3] from the code it was working fine then what is the point of new declaration for dynamic arrays.
//Code : Dynamic arrays
module darr;
int da[];
initial begin
da=new[3];
$display("array size = %0d",da.size());
da='{2,3,4,5,6,7,8,9,10};
foreach (da[i]) $display("array[%0d] = %0d", i, da[i]);
$display("array size = %0d",da.size());
da.delete;
$display("size of array da after delete : %0p",da.size());
end
endmodule
Output Printed :
# run -all
# array size = 3
# array[0] = 2
# array[1] = 3
# array[2] = 4
# array[3] = 5
# array[4] = 6
# array[5] = 7
# array[6] = 8
# array[7] = 9
# array[8] = 10
# array size = 9
# size of array da after delete : 0
# exit
Output if da=new[3] is commented :
# run -all
# array size = 0
# array[0] = 2
# array[1] = 3
# array[2] = 4
# array[3] = 5
# array[4] = 6
# array[5] = 7
# array[6] = 8
# array[7] = 9
# array[8] = 10
# array size = 9
# size of array da after delete : 0
# exit