1. array
Creates a new array of specified length with elements initialized to null.
1.2. Stack Effects
| Level | Object |
|---|---|
0 |
|
| Level | Object |
|---|---|
0 |
|
1.3. Description
array creates a new array object of length int, with each element initialized to null. The array is allocated in local or global VM according to the current VM allocation mode.
The int operand must be:
-
A non-negative integer
-
Not greater than the maximum allowable array length (implementation-dependent)
1.5. Examples
3 array % Creates [null null null]
3 array % [null null null]
dup 0 42 put % [42 null null]
dup 1 (hello) put % [42 (hello) null]
dup 2 /name put % [42 (hello) /name]
5 array % Create 5-element array
0 1 4 { % Loop from 0 to 4
2 copy % Duplicate index and array
exch % index array → array index
dup 10 mul % Multiply index by 10
put % Store in array
} for
% Result: [0 10 20 30 40]
1.6. Common Use Cases
1.7. Common Pitfalls
Uninitialized Elements - Remember that array initializes all elements to null. You must explicitly populate the array before using its elements.
|
3 array
0 get % Returns null, not an error
| VM Allocation - Arrays are allocated in VM. Creating many large arrays can exhaust available memory. |
Array Literals vs. array Operator - For known, fixed-size arrays, use the literal syntax [ … ] instead. Use array when the size is computed dynamically.
|
1.8. Error Conditions
| Error | Condition |
|---|---|
[ |
int exceeds implementation’s maximum array length |
[ |
int is negative |
[ |
Fewer than 1 operand on stack |
[ |
Operand is not an integer |
[ |
Insufficient VM to allocate array |
1.9. Implementation Notes
-
Most PostScript implementations support arrays up to at least 65,535 elements
-
Array allocation is typically faster for small arrays
-
Consider using
packedarrayfor read-only data (Level 2)