1. array

Creates a new array of specified length with elements initialized to null.

1.1. Syntax

int array → array

1.2. Stack Effects

Table 1. Before
Level Object

0

int (non-negative integer)

Table 2. After
Level Object

0

array (new array of length int)

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.4. PostScript Level

Level 1 and later

1.5. Examples

Creating a simple array
3 array  % Creates [null null null]
Creating and populating an array
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]
Dynamic array with loop
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.6.1. Building Data Structures

% Create a point structure
2 array
dup 0 100 put  % x coordinate
dup 1 200 put  % y coordinate

1.6.2. Temporary Storage

% Store interim calculation results
10 array
% ... fill with computed values ...

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

[limitcheck]

int exceeds implementation’s maximum array length

[rangecheck]

int is negative

[stackunderflow]

Fewer than 1 operand on stack

[typecheck]

Operand is not an integer

[VMerror]

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 packedarray for read-only data (Level 2)

1.10. Performance Considerations

  • Creating many small arrays is more efficient than a few large ones

  • Pre-allocating arrays of known size is more efficient than growing them dynamically

  • For large datasets, consider using dictionaries as an alternative

1.11. See Also

  • [ - Begin array construction

  • ] - End array construction

  • astore - Store stack elements into array

  • packedarray - Create read-only packed array (Level 2)

  • length - Get array length

  • get - Get array element

  • put - Put value into array


Back to top

Copyright © 2025 Ribose. PostScript is a trademark of Adobe. Distributed under the MIT License.