Arrays as argument

Jan. 12, 2024 || Item:2.6.Arrays as argument

Arrays can be passed as arguments to tasks or functions. The rules that govern array argument passing by value are the same as for array assignment. When an array argument is passed by value, a copy of the array is passed to the called task or function. This is true for all array types: fixed-size, dynamic, or associative.

Note that unsized dimensions can occur in dynamic arrays and in formal arguments of import DPI functions. If one dimension of a formal is unsized, then any size of the corresponding dimension of an actual is accepted.

For example, the declaration:

task fun(int a[3:1][3:1]);

declares task fun that takes one argument, a two dimensional array with each dimension of size three. A call to fun must pass a two dimensional array and with the same dimension size 3 for all the dimensions. For example, given the above description for fun, consider the following actuals:

int b[3:1][3:1];    // OK: same type, dimension, and size

int b[1:3][0:2];    // OK: same type, dimension, & size (different ranges)

reg b[3:1][3:1];    // OK: assignment compatible type

event b[3:1][3:1];  // error: incompatible type

int b[3:1];         // error: incompatible number of dimensions

int b[3:1][4:1];    // error: incompatible size (3 vs. 4)

A subroutine that accepts a one-dimensional fixed-size array can also be passed a dynamic array of a compatible type of the same size.

For example, the declaration:

task bar( string arr[4:1] );

declares a task that accepts one argument, an array of 4 strings. This task can accept the following actual arguments:

string b[4:1];        // OK: same type and size

string b[5:2];        // OK: same type and size (different range)

string b[] = new[4]; // OK: same type and size, requires run-time check

A subroutine that accepts a dynamic array can be passed a dynamic array of a compatible type or a one-dimensional fixed-size array of a compatible type

For example, the declaration:

task foo( string arr[] );

declares a task that accepts one argument, a dynamic array of strings. This task can accept any one-dimensional array of strings or any dynamic array of strings.

An import DPI function that accepts a one-dimensional array can be passed a dynamic array of a compatible type and of any size if formal is unsized, and of the same size if formal is sized. However, a dynamic array can- not be passed as an argument if formal is an unsized output.

 



Comments:

Leave a comment: