Duplicates the top element on the operand stack, creating a second copy.
1. Description
The dup operator creates a copy of the topmost element on the operand stack and pushes it onto the stack. This is one of the most frequently used stack manipulation operators in PostScript.
Important: dup copies only the object itself, not the value of composite objects. For arrays, dictionaries, and strings, both the original and the duplicate reference the same underlying value. Changes to the value through either reference will be visible through both.
This is a Level 1 operator, available in all PostScript implementations.
4. Return Values
Two identical references to the same object. For composite objects, both references share the same underlying value.
5. Examples
5.1. Basic Usage
% Duplicate a number
42 dup % Stack: 42 42
% Duplicate a string
(hello) dup % Stack: (hello) (hello)
% Use duplicate for calculation
5 dup mul % Stack: 25 (5 squared)
5.2. Using Both Copies
% Calculate x^2 + x
10 dup % Stack: 10 10
dup mul % Stack: 10 100
exch % Stack: 100 10
add % Stack: 110
6. Advanced Examples
6.1. Multiple Duplication
% Create three copies of a value
/triple { % any -> any any any
dup dup
} def
5 triple % Stack: 5 5 5
6.2. Safe Division with Check
% Divide, but check for zero divisor
/safeDivide { % numerator denominator -> result
dup 0 eq {
pop pop % Remove both operands
(Error: Division by zero) print
0 % Return 0 or some default
} {
div
} ifelse
} def
10 2 safeDivide % Stack: 5.0
10 0 safeDivide % Prints error, stack: 0
7. Edge Cases and Common Pitfalls
Using dup on an empty stack causes a stackunderflow error.
|
7.2. Composite Object Pitfall
% BAD: Unexpected sharing
[1 2 3] dup % Stack: [1 2 3] [1 2 3]
0 99 put % Modify "first" array
% Stack: [99 2 3] [99 2 3]
% BOTH arrays are modified!
% GOOD: Create independent copy when needed
[1 2 3]
dup length array copy % True independent copy
0 99 put % Only one array modified
Remember that dup creates a shallow copy. For composite objects (arrays, dictionaries, strings), use the copy operator when you need an independent copy of the value.
|
9. PostScript Level
Available in: PostScript Level 1 and higher
This is a fundamental operator available in all PostScript implementations.
10. Error Conditions
stackunderflow-
The operand stack is empty when
dupis executed. There must be at least one element on the stack.clear dup % ERROR: stackunderflow stackoverflow-
The operand stack has reached its maximum capacity and cannot accommodate another element.
% (Extremely rare in practice) % Occurs only if stack is nearly full
11. Performance Considerations
The dup operator is extremely fast and has negligible performance impact. It’s a basic stack operation with O(1) constant time complexity.
For composite objects, dup only copies the reference, not the entire value, making it very efficient even for large arrays or dictionaries.
12. Best Practices
-
Use for calculations:
dupis essential when you need the same value multiple times in a calculation -
Understand object vs. value: Remember that
dupcopies the object (reference), not the value for composite types -
Document stack effects: Clearly document when procedures use
dupto duplicate values -
Consider copy for independence: Use
copywhen you need an independent copy of a composite object -
Combine with conditionals: The pattern
dup … { … } { … } ifelseis very common
13. See Also
-
Operators Overview - Understanding PostScript operators
-
Stack Operations Guide - Stack manipulation tutorial
-
Composite Objects - Understanding object vs. value copying
-
Stack Manipulation - All stack operators