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.

2. Syntax

any dup any any

2.1. Stack Effect

Table 1. Before Execution
Position Content

Top

any (any type) - The element to duplicate

Table 2. After Execution
Position Content

Top

any - Copy of the original element

Top-1

any - The original element

3. Parameters

any

Any PostScript object of any type. The object will be duplicated on the stack.

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

5.3. Composite Object Sharing

% Arrays share values
[1 2 3] dup     % Stack: [1 2 3] [1 2 3]
0 99 put        % Modify first array
                % Both arrays now: [99 2 3]

% To make independent copy, use copy instead
[1 2 3]
dup length array copy   % True independent copy

5.4. Common Pattern with Operators

% Keep value while testing it
/x 100 def
x dup 0 gt {    % Test if positive
    % Use duplicated value
    sqrt        % Take square root
} {
    pop         % Discard if negative
    0           % Return 0 instead
} ifelse

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

6.3. Preserving Value for Multiple Operations

% Calculate both sin and cos of an angle
/sinCos {       % angle -> sin cos
    dup         % angle angle
    sin         % angle sin
    exch        % sin angle
    cos         % sin cos
} def

45 sinCos       % Stack: 0.707... 0.707...

6.4. Building Complex Structures

% Create a dictionary with self-reference
/makeCircular {
    5 dict dup              % dict dict
    begin
        /self currentdict def
    end
} def

7. Edge Cases and Common Pitfalls

Using dup on an empty stack causes a stackunderflow error.

7.1. Empty Stack

% BAD: No elements to duplicate
clear
dup             % ERROR: stackunderflow

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.

7.3. Dictionary Sharing

% Be aware of shared dictionary values
5 dict dup              % Both references point to same dictionary
/a 1 put                % Affects both references
% Both dict references now have entry a:1
  • exch - Exchange top two elements

  • copy - Copy multiple elements or create independent copies

  • index - Duplicate nth element from top

  • roll - Rotate stack elements

  • pop - Remove top element

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 dup is 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

  1. Use for calculations: dup is essential when you need the same value multiple times in a calculation

  2. Understand object vs. value: Remember that dup copies the object (reference), not the value for composite types

  3. Document stack effects: Clearly document when procedures use dup to duplicate values

  4. Consider copy for independence: Use copy when you need an independent copy of a composite object

  5. Combine with conditionals: The pattern dup …​ { …​ } { …​ } ifelse is very common

12.1. Good Stack Management

% Document when values are duplicated
/square {       % x -> x^2
    dup mul
} def

/cube {         % x -> x^3
    dup dup mul mul
} def

% Clear documentation prevents confusion
/pythagorean {  % x y -> sqrt(x^2 + y^2)
    dup mul     % x y^2
    exch        % y^2 x
    dup mul     % y^2 x^2
    add sqrt    % result
} def

13. See Also


Back to top

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