Dynamic Array- System Verilog

Doubt 1: Even though without creating a memory the array is taking the value, then what is the point of dynamic array?

Ref. to Doubt 1:

module ex;
int x [ ];
initial begin

//Here i am not creating the memory
$display(“size=%0d”,x.size());
x = '{1,2,3,4,5,6,7};
$display(“x=%p”,x);
$display(“size=%0d”,x.size());
end
endmodule

Doubt 2: While assigning the elements to the array, here I had created the size 3 and assign some 7 elements, and its working.

But, after assigning the value using foreach loop, and the if i try to assign to the out of bound value then it is displaying 0.

Ref. to Doubt 2:

module ex;
int x [ ];
initial begin

x=new[3]                                   //Size =3
$display(“size=%0d”,x.size());
x = '{1,2,3,4,5,6,7};                 // assigning more elements greater than the size
$display(“x=%p”,x);
$display(“size=%0d”,x.size());
end
endmodule

The code in your first question doesn’t match the output you provided. It matches the code in the second question. Additionally, the code in the second question doesn’t contain a foreach loop.

In any case, whenever you copy an entire aggregate array to a dynamic array, there’s no need to construct (call new[n]) beforehand. The previous contents of the array are deleted and replaced with the right-hand side of the assignment. You would only call the constructor first if your intention was to access elements individually.

Once you’ve constructed a dynamic array, it behaves similarly to any other array when accessing individual elements. Accessing out-of-bounds indexes doesn’t do anything, and reads return the default value for the element type.