LifetimeĪn array created by an open array constructor is only valid as long as the function or procedure runs and is discarded right afterward. Here, the array is defined on the spot as. The above example with the NonZero array could also have been written like this: Luckily, Delphi allows you to declare open arrays on the spot, using the so called open array constructor syntax, which uses to define the array. Sometimes you don’t want to declare and fill an array just so you can use it with an open array parameter. So if you don’t need to modify items in the array locally, make the open array parameter const. For large arrays, this can be very inefficient.
The array is simply passed by reference, but if it is not declared const, the hidden start code of the routine will allocate room on the stack and copy the entire array to that local storage, using the reference as source address. Open array parameters that are not passed as const will entirely be copied into local storage of the routine. Usually, you can pass open arrays as const parameters. For arrays of a static size, like array of Integer, it uses the declared size to pass the High value for dynamic arrays, it compiles code to get the High value of the array at runtime. Procedure ListAllIntegers ( const AnArray : Pointer High : Integer ) Įach time you pass an array to an open array parameter, the compiler, which knows the size of the array, will pass its address and its adjusted High value to the procedure or function.