LBOUND / UBOUND
Syntax
Syntax:
Source:
LBOUND(array) | UBOUND(array)Source:
pb_cg_emit_bound_builtin (pb_codegen.c:4176)Description
LBOUND returns the lowest valid index (lower bound) of an array. UBOUND returns the highest valid index (upper bound). These are essential for writing loops that iterate over arrays without hard-coding index ranges.
- For fixed arrays the bounds are compile-time constants. The compiler emits an immediate
MOV. - For dynamic arrays (REDIM) the bounds are stored in the array descriptor at runtime and are loaded from the stack frame.
Parameters
| Parameter | Description |
|---|---|
array | Any array variable (fixed or dynamic) |
Example
LOCAL a(1 TO 10) AS LONG
LOCAL i AS LONG
FOR i = LBOUND(a) TO UBOUND(a)
a(i) = i * 2
NEXT i
REDIM d(5 TO 15) AS LONG LOCAL lo, hi AS LONG lo = LBOUND(d) ' lo = 5 hi = UBOUND(d) ' hi = 15
Dynamic Array (REDIM)
; LBOUND: load from descriptor in stack frame mov rax, [rbp - lower_offset] ; runtime lower bound ; UBOUND: load from descriptor mov rax, [rbp - upper_offset] ; runtime upper bound
Fixed Array
; LBOUND: compile-time constant mov rax, <constant_lower> ; e.g., 0 or 1 ; UBOUND: compile-time constant mov rax, <constant_upper> ; e.g., 10
Array Descriptor Layout (per array in stack frame)
; Dynamic array descriptor: [rbp - ptr_offset] ; pointer to heap data (from malloc) [rbp - lower_offset] ; lower bound (from REDIM) [rbp - upper_offset] ; upper bound (from REDIM) [rbp - count_offset] ; element count [rbp - capacity_offset] ; allocated capacity [rbp - stride_offset] ; bytes per element ; Fixed array: just the data inline [rbp - base + i*stride] ; elements laid out contiguously