ch2-Dynamic Arrays
2019-10-24
SystemVerilog Dynamic Array
A dynamic array is an unpacked array whose size can be set or changed at run time, and hence is quite different from a static array where the size is pre-determined during declaration of the array. The default size of a dynamic array is zero until it is set by the new()
constructor.
Syntax
A dynamic array dimensions are specified by the empty square brackets [ ]
.
1 |
|
The new()
function is used to allocate a size for the array and initialize its elements if required.
Dynamic Array Example
1 |
|
ynamic Array Methods
Function | Description |
---|---|
function int size (); | Returns the current size of the array, 0 if array has not been created |
function void delete (); | Empties the array resulting in a zero-sized array |
1 |
|
How to add new items to a dynamic array ?
Many times we may need to add new elements to an existing dyanmic array without losing its original contents. Since the new()
operator is used to allocate a particular size for the array, we also have to copy the old array contents into the new one after creation.
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!