Controlling out of bound in static arrays

Controlling out of bounds index error

By default, when an array index is out of range, fglrun raises error -1326. This is only the case for static arrays. When using a dynamic array, new elements are allocated if the index is greater than the actual array size.

Raising an index out of bounds error is normal for static arrays. However, in some situations, code must execute without error and evaluate expressions using indexes that are greater than the size of the array, especially with boolean expressions in IF statements:

IF index <= max_index OR arr[index] == some_value THEN
   ...
END IF

In this example, as all parts of a boolean expression need to be evaluated, the runtime system must get the value of the arr[index] element.

You can use an FGLPROFILE entry to control the behavior of the runtime system when an array index is out of bounds for a static array:

fglrun.arrayIgnoreRangeError = true

When this FGLPROFILE entry is set to true, the runtime system will return the first element of the array if the index is <=0 or greater than the size of the array and continue with the normal program flow.

Unless existing code is relying on this behavior, it is better to let the default get array out of bounds errors when the index is invalid.

You may also want to use the compiler directive to control boolean expression evaluation, with the OPTIONS SHORT CIRCUIT instruction.