What is an array data type?
The array is the collection of the same data types.
Example:
int array [8] //a collection of 8 int data.
Fixed Size Array
An array with a fixed size of its elements throughout the simulation is known as a fixed-size array.
Example: int array [5] // it stores 5 elements of type int. (compact declaration)
Code: fixed size array
module tb;
byte array[3]; //fixed array of byte declaration
initial begin
//assign the values
array[0]=1;
array[1]=2;
array[2]=3;
//print the values of each element
$display("array[0]=%0d \t array[1]=%0d \t array[2]=%0d", array[0],array[1],array[2]);
//you can also print the new array size and element using foreach.
foreach(array[i]) begin
$display("array[%0d]=%0d" ,i,array[i]);
end
end
endmodule
What is the difference between a packed and unpacked array?
Packed:
The size is described before the array’s name so it is an unpacked array.
Bit [7:0] array; // packed array
Unpacked:
The size is described after the array’s name so it is an unpacked array.
An array of byte, shortint, int are all stored in a single longword, while a longint is stored in two long words. Memory utilization will poor(waste) if we can not use it with proper planning.
Examples:
byte array[4]; // Unpacked array
What are single-dimension and multi-dimension arrays?
In single-dimension array can have only one list of elements, while an array can have multiple lists of elements.
What is a dynamic array?
When you do not know the size of the array until run-time, a better way is to use the dynamic array. It can be modified at run-time. The array must be initialized empty and call the new[] operator for allotment.
Example:
module tb;
int array[]; //dynamic array declaration
initial begin
array=new[3]; //size of array 3
//assign the values
array[0]=1;
array[1]=2;
array[2]=3;
//print the size of the array using $size function
$display("size of the array=%0d ", array.size());
//print the values of each element
$display("array[0]=%0d \t array[1]=%0d \t array[2]=%0d", array[0],array[1],array[2]);
//modify the size of the array without affecting the previous values.
array=new[5](array);
//assign the value
array[3]=4;
array[4]=5;
//print the new array size and element using foreach loop.
foreach(array[i]) begin
$display("array size= %0d, array[%0d]=%0d" , array.size(),i,array[i]);
end
//detete all the array elements
array.delete();
end
endmodule
What are single-dimension and multi-dimension arrays?