Removes the top element from the operand stack and discards it.

1. Description

The pop operator removes and discards the topmost element from the operand stack. It is one of the most fundamental stack manipulation operators and is frequently used to discard unwanted results or clean up the stack after operations.

This is a Level 1 operator, available in all PostScript implementations.

2. Syntax

any pop -

2.1. Stack Effect

Table 1. Before Execution
Position Content

Top

any (any type) - The element to be removed and discarded

Table 2. After Execution
Position Content

(Stack is one element shorter)

3. Parameters

any

Any PostScript object of any type. The object is removed from the stack and discarded.

4. Return Values

None. The pop operator consumes one element and produces no results.

5. Examples

5.1. Basic Usage

% Push some values
10 20 30

% Remove top value (30)
pop

% Stack now contains: 10 20
count =        % Prints: 2

5.2. Discarding Intermediate Results

% Calculate but discard intermediate result
5 6 mul        % Stack: 30
dup            % Stack: 30 30
7 add          % Stack: 30 37
exch           % Stack: 37 30
pop            % Stack: 37 (discarded the 30)

5.3. Cleaning Up After Operations

% Function that leaves extra value
/calculate {
    10 20 add  % Returns 30
    100        % Accidentally leaves 100 on stack
} def

% Use it and clean up
calculate      % Stack: 30 100
pop            % Stack: 30 (cleaned up unwanted 100)

5.4. Multiple Pops

% Remove multiple values
1 2 3 4 5      % Stack: 1 2 3 4 5
pop pop        % Stack: 1 2 3

6. Advanced Examples

6.1. Conditional Stack Cleanup

% Remove value only if condition is true
/conditionalPop {  % bool any -> any|nothing
    exch           % any bool
    {              % If true
        pop        % Remove the value
    } {            % If false
        % Leave value on stack
    } ifelse
} def

% Usage
42 true conditionalPop   % Stack is empty (value removed)
42 false conditionalPop  % Stack: 42 (value kept)

6.2. Stack Depth Management

% Ensure stack has exactly n elements
/ensureDepth {  % any1 ... anyx n -> any1 ... anyn
    count 1 index sub  % Calculate how many to remove
    {
        dup 0 gt {     % While extras exist
            exch pop   % Remove one
            1 sub      % Decrement counter
        } {
            pop exit   % Done
        } ifelse
    } loop
} def

7. Edge Cases and Common Pitfalls

Using pop on an empty stack causes a stackunderflow error.

7.1. Empty Stack

% BAD: This will error
clear          % Empty the stack
pop            % ERROR: stackunderflow

Always ensure the stack has elements before using pop:

% GOOD: Check stack depth first
count 0 gt {   % If stack not empty
    pop        % Safe to pop
} if

7.2. Accidentally Removing Important Values

% BAD: Lost the value we needed
10 20 add      % Stack: 30
pop            % Stack: empty - oops!
% Value is gone forever
Use dup before pop if you might need the value:
% BETTER: Keep a copy
10 20 add      % Stack: 30
dup            % Stack: 30 30
someOperation  % Uses one copy
pop            % Removes result if not needed
% Original 30 still available
  • exch - Exchange top two elements instead of removing

  • dup - Duplicate top element

  • clear - Remove all elements from stack

  • roll - Rotate elements (can achieve similar effects)

  • count - Check stack depth before popping

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 pop is executed. There must be at least one element on the stack.

clear
pop     % ERROR: stackunderflow

11. Performance Considerations

The pop operator is extremely fast and has negligible performance impact. It’s a basic stack operation with O(1) constant time complexity.

12. Best Practices

  1. Check before popping: Use count to verify stack depth in defensive code

  2. Document side effects: If a procedure uses pop, document it clearly

  3. Prefer specific operations: Sometimes exch pop or other combinations are clearer than just pop

  4. Avoid excessive popping: If you’re popping many values, consider whether your stack management could be improved

12.1. Good Stack Discipline

% Document stack effects clearly
/myProcedure {  % x y z -> result
    % Takes three arguments, returns one
    add add       % Sum all three
    % Stack automatically cleaned: consumed x, y, z; produced result
} def

13. See Also


Back to top

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