Home » , » Dynamic arrays

Dynamic arrays

Written By 1 on Thursday, January 16, 2014 | 8:11 AM

Dynamic Arrays

A dynamic array is one dimension of an unpacked array whose size can be set or changed at runtime. The space for a dynamic array doesn’t exist until the array is explicitly created at runtime.
The syntax to declare a dynamic array is:
data_type array_name [];
where data_type is the data type of the array elements. Dynamic arrays support the same types as fixed-size arrays.
For example:
bit [3:0] nibble[]; // Dynamic array of 4-bit vectors
integer mem[]; // Dynamic array of integers
The new[] operator is used to set or change the size of the array.
The size() built-in method returns the current size of the array.
The delete() built-in method clears all the elements yielding an empty array (zero size).
New()
The built-in function new allocates the storage and initializes the newly allocated array elements either to their default initial value or to the values provided by the optional argument.
dynamic_array_new ::=new [ expression ] [ ( expression ) ]
integer addr[]; // Declare the dynamic array.
addr = new[100]; // Create a 100-element array....
// Double the array size, preserving previous values.
addr = new[200](addr);
The new operator follows the SystemVerilog precedence rules. Since both the square brackets [] and the parenthesis () have the same precedence, the arguments to this operator are evaluated left to right:
[ expression ] first, and ( expression ) second.
size()
The prototype for the size() method is:
function int size();
The size() method returns the current size of a dynamic array, or zero if the array has not been created.
delete()
The prototype for the delete() method is:
function void delete();
The delete() method empties the array, resulting in a zero-sized array.
int ab [] = new[ N ]; // create a temporary array of size N
blocking_assignment ::=|
hierarchical_dynamic_array_variable_identifier = dynamic_array_new.
new [ expression ] [ ( expression ) ]
// use ab
ab.delete; // delete the array contents
$display( "%d", ab.size ); // prints 0

0 Comment:

Post a Comment