Arrays
Arrays are allocated with new, carry their length in a header and are bounds-checked on every access. Fixed-size arrays have their dimensions in the type; dynamic arrays learn them at runtime.
Fixed-size arrays
#include <libstd/stdio>
def sum3(a: int[3]) -> int:
return a[0] + a[1] + a[2]
def main() -> int:
arr = new int[3] // type: int[3], zero-initialised
arr[0] = 1
arr[1] = 2
arr[2] = 3
print(sum3(arr), "\n") // 6
return 0
main()
int[3] and int[5] are different types: a function taking int[3] accepts only arrays of exactly three elements, which the compiler checks. Elements can be any scalar (int, long, char, float, ...), a str, or a class instance stored by value.
Multidimensional arrays
grid = new int[3][4]
grid[1][2] = 7
Each row of a fixed int[3][4] is itself an int[4] with its own length header, so grid[i][j] checks i against 3 and j against 4.
Dynamic arrays
When a dimension is a variable, the array becomes dynamic: a single self-describing header (depth, element size, each dimension) followed by all the elements in row-major order. The parameter type for a dynamic array is T[].
def fill(m: int[], rows: int, cols: int) -> None:
i = 0
while i < rows:
j = 0
while j < cols:
m[i][j] = i * cols + j
j++
i++
n = 3
k = 4
m = new int[n][k]
fill(m, n, k)
Indexing a dynamic array with fewer or more indices than its depth is not checked at compile time (the type does not know the depth): it is the caller's responsibility.
Bounds checking
Every index on an array is compared with the length stored in its header (rt_check_array_bounds). An out-of-range index prints Error! array index out of bounds and stops the program with the crash report described in Memory safety, never a silent read of the neighbouring memory.
arr = new int[4]
idx = 9
print(arr[idx])
Error! array index out of bounds
==================== CRASH ====================
Segnale n.: 000000000000000B
...
unsafe: blocks
Inside an unsafe: block array accesses skip the bounds check. The block is a plain statement list (no scope of its own) and can be nested. It only removes the index check: allocation, promotion and every other guarantee are unchanged.
unsafe:
i = 0
while i < n:
total = total + data[i] // no rt_check_array_bounds here
i++
The compiler also elides the check on its own when the index is provably in range (a loop variable bounded by the array length), so unsafe: is rarely needed for speed.
List literals
A bracketed literal creates a fixed array of the common type of its elements; mixing int and long widens everything to long.
lista = [10, 20, 30] // int[3]
big = 200000000000 as long
mixed = [1, big, 3] // long[3]
For a growable sequence use List from libstd/list, described in Lists & dicts.
Arrays in classes
A field can be a fixed array (valori int[5]): the space is reserved inline in the object, no separate allocation, and the same bounds check applies. An array of class instances (vertici Point[3]) stores the instances by value. See Classes.
Memory layout
The variable holds a pointer to the header, not to the first element; the header stays part of the tracked allocation, which is what lets the arena find the array again when it has to free or promote it. Details in The arena.
fixed new int[4] : [length 8B][e0 4B][e1][e2][e3]
fixed new int[3][4] : [3][ row: [4][e0..e3] ][ row ][ row ]
dynamic new int[n][m] : [depth 4B][elem_size 4B][n 4B][m 4B][n*m elements]