1. maxlength

Returns the maximum capacity of a dictionary.

1.1. Syntax

dict maxlength → int

1.2. Stack Effects

Table 1. Before
Level Object

0

dict (dictionary)

Table 2. After
Level Object

0

int (maximum capacity)

1.3. Description

maxlength returns the capacity of dict - the maximum number of key-value pairs it can hold using the VM currently allocated to it.

Level 1: Returns the int operand originally passed to dict - the dictionary’s fixed maximum capacity.

Level 2: Returns the current capacity, which may be larger than the original size if the dictionary has grown.

1.4. PostScript Level

Level 1 and later

1.5. Examples

Basic capacity query
/mydict 5 dict def
mydict length     % Returns 0 (empty)
mydict maxlength  % Returns 5 (capacity)
Dictionary growth (Level 2)
2 dict begin
  /k1 1 def
  /k2 2 def
  /k3 3 def  % Auto-expands in Level 2
  currentdict maxlength  % Returns > 2
end
Checking available space
mydict dup maxlength
exch length sub  % Available slots

1.6. Common Use Cases

1.6.1. Pre-allocation Check

dict dup maxlength exch length sub
requiredSlots lt {
  % Need bigger dictionary
  dup length requiredSlots add dict
  copy
} if

1.6.2. Memory Planning

/estimateMemory {
  dict maxlength 20 mul  % Rough estimate: 20 bytes per entry
} def

1.6.3. Dictionary Debugging

/dictInfo {
  dup length =only ( / ) print
  maxlength =only ( entries\n) print
} def

1.7. Common Pitfalls

maxlength ≠ length - maxlength is capacity, length is current count.
5 dict
dup length     % 0 (no entries yet)
maxlength      % 5 (capacity)
Level Differences - Behavior differs between Level 1 and 2:
% Level 1: Fixed at creation
% Level 2: Can grow (returns current capacity)
Don’t Rely on Exact Values - In Level 2, maxlength may increase unpredictably due to automatic expansion.

1.8. Error Conditions

Error Condition

[invalidaccess]

Dictionary has no-access attribute

[stackunderflow]

No operand on stack

[typecheck]

Operand is not a dictionary

1.9. Implementation Notes

Level 1: * Returns fixed capacity set at creation * Never changes * Simple field lookup

Level 2: * Returns current allocated capacity * May be larger than original size * Capacity increases in chunks when dictionary grows

1.10. Capacity vs. Usage

Measurement Operator Meaning

Current entries

length

Number of key-value pairs

Maximum capacity

maxlength

Slots available with current VM allocation

Available space

maxlength length sub

Remaining capacity (Level 1)

1.11. See Also

  • length - Get current entry count

  • dict - Create dictionary with initial capacity

  • def - Add entry to dictionary

  • known - Check if key exists


Back to top

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