<p><a href="https://github.com/bitcoin/bitcoin/blob/25cc1cf8dc03b9ba0ae886d354855adc207b5b6e/src/script/interpreter.cpp#L308" rel="noreferrer">Here's where they're defined:</a></p>

<pre><code>            case OP_TOALTSTACK:
            {
                if (stack.size() &lt; 1)
                    return false;
                altstack.push_back(stacktop(-1));
                popstack(stack);
            }
            break;

            case OP_FROMALTSTACK:
            {
                if (altstack.size() &lt; 1)
                    return false;
                stack.push_back(altstacktop(-1));
                popstack(altstack);
            }
            break;
</code></pre>

<p>Example:</p>

<pre><code>Stack:
Alternate Stack:
Script: OP_1 OP_2 OP_TOALTSTACK
</code></pre>

<p>.</p>

<pre><code>Stack: 01
Alternate Stack:
Script: OP_2 OP_TOALTSTACK
</code></pre>

<p>.</p>

<pre><code>Stack: 01 02
Alternate Stack:
Script: OP_TOALTSTACK
</code></pre>

<p>.</p>

<pre><code>Stack: 01
Alternate Stack: 02
Script: 
</code></pre>

<blockquote>
  <p>what is it for? Do any specific use cases come to mind?</p>
</blockquote>

<p>It's... use-impaired. 99% of the time, you can avoid using OP_(TO|FROM)ALTSTACK by putting things onto the stack in a different order. There are 18 stack manipulation operators, but only one (OP_DUP) is used with any regularity. I'm of the opinion that Bitcoin scripting is overcomplicated for its intended use.</p>

CC BY-SA 3.0
2014-10-26T16:08:00.857Z
+1, but in stack-based languages, whether or not necessary, it is often convenient to have access to a second stack.  For example, in Forth, it is a common idiom to (ab)use the return stack (`>R`, `R>`) for storing temporary data.  See http://www.forth.com/starting-forth/sf5/sf5.html for an example.  The designers may have had this in mind.  Anyway, given the difficulty (chain fork) of adding additional features to the scripting language, one can understand that the designers had an incentive to overdesign.
Nate Eldredge
CC BY-SA 3.0
2014-10-27T17:28:07.617Z
@Nick, I see where you're coming from when there are really only a few types of standard transactions, but some think the Bitcoin scripting language is too simple for its intended use. e.g. Ethereum. I think Satoshi found a good balance with the Bitcoin scripting language, making it flexible but also statically analyzable.
morsecoder
CC BY-SA 3.0
2014-10-26T14:47:31.407Z
2014-10-26T17:09:28.917Z
2014-10-26T17:09:28.917Z
Nick ODell
<p>I noticed in the bitcoin wiki script page (<a href="https://en.bitcoin.it/wiki/Script">https://en.bitcoin.it/wiki/Script</a>) that there were two opcodes named:</p>

<pre><code>OP_TOALTSTACK
OP_FROMALTSTACK
</code></pre>

<p>It looks like there is another stack that can be used separately as a data store, simply pushing and popping data. I've never heard of this before, but it seems like a very useful feature. </p>

<p>Can anyone verify that this is what the alt stack is actually meant to be used for? If not for just pushing and popping data, what is it for? Do any specific use cases come to mind?</p>

<script>
What is the alt stack?
morsecoder
# What is the alt stack?

(Asked by: username_0 on 2014-10-26)

I noticed in the bitcoin wiki script page (<https://en.bitcoin.it/wiki/Script>) that there were two opcodes named:

```
OP_TOALTSTACK
OP_FROMALTSTACK

```

It looks like there is another stack that can be used separately as a data store, simply pushing and popping data. I've never heard of this before, but it seems like a very useful feature.

Can anyone verify that this is what the alt stack is actually meant to be used for? If not for just pushing and popping data, what is it for? Do any specific use cases come to mind?

**Tags:** script


---

## 1 answers

### Answer by username_1 on 2014-10-26. Score: 9 | Accepted answer

[Here's where they're defined:](https://github.com/bitcoin/bitcoin/blob/25cc1cf8dc03b9ba0ae886d354855adc207b5b6e/src/script/interpreter.cpp#L308)

```
            case OP_TOALTSTACK:
            {
                if (stack.size() < 1)
                    return false;
                altstack.push_back(stacktop(-1));
                popstack(stack);
            }
            break;

            case OP_FROMALTSTACK:
            {
                if (altstack.size() < 1)
                    return false;
                stack.push_back(altstacktop(-1));
                popstack(altstack);
            }
            break;

```

Example:

```
Stack:
Alternate Stack:
Script: OP_1 OP_2 OP_TOALTSTACK

```

.

```
Stack: 01
Alternate Stack:
Script: OP_2 OP_TOALTSTACK

```

.

```
Stack: 01 02
Alternate Stack:
Script: OP_TOALTSTACK

```

.

```
Stack: 01
Alternate Stack: 02
Script: 

```

> what is it for? Do any specific use cases come to mind?

It's... use-impaired. 99% of the time, you can avoid using OP\_(TO|FROM)ALTSTACK by putting things onto the stack in a different order. There are 18 stack manipulation operators, but only one (OP\_DUP) is used with any regularity. I'm of the opinion that Bitcoin scripting is overcomplicated for its intended use.

> **Comments:**
> **username_2**: +1, but in stack-based languages, whether or not necessary, it is often convenient to have access to a second stack.  For example, in Forth, it is a common idiom to (ab)use the return stack (`>R`, `R>`) for storing temporary data.  See http://www.forth.com/starting-forth/sf5/sf5.html for an example.  The designers may have had this in mind.  Anyway, given the difficulty (chain fork) of adding additional features to the scripting language, one can understand that the designers had an incentive to overdesign.  
> **username_0**: @username_1, I see where you're coming from when there are really only a few types of standard transactions, but some think the Bitcoin scripting language is too simple for its intended use. e.g. Ethereum. I think Satoshi found a good balance with the Bitcoin scripting language, making it flexible but also statically analyzable.  

---

