Have you ever needed to keep an incrementing counter, but ran out of variables to clobber? Then I have the perfect solution for you! This is a trick that Big Calculator Tutorial doesn't want you to know - for years, they've warned against this technique, claiming it to be too dangerous. But today, I'll reveal the secret of End-Stack-As-Data-Storage!
It's well known that if you use Goto to exit a block, you'll leak memory. But have you ever wondered what this memory actually is? The OS keeps track of the blocks that it's in on the operator stack so that it knows what to do when it encounters an End statement. Whenever it sees a loop command, it pushes something to the stack that tells it where it which statement to start over from for the next iteration. When it encounters a Then block that it takes, it pushes something that says that it's a Then statement, so that it doesn't try to loop back anywhere when it hits an End statement. Normally, this works fine for keeping track of blocks, but if you use Goto, you can skip over an End statement, leaving an extra element on the stack. For Then statements, this usually just means that the memory gets leaked, but when you also Goto out of loops, you can do some interesting stuff with it.
For example, here's a program that counts how many iterations of a loop pass between the time that you start the program and the time that you press a key:
Code:
Notice that at no point during the main loop (the second block) is the iteration count stored in a variable. Instead, it's kept track of using the depth of the stack. The next loop pops Then blocks from the stack until it reaches the For( block from the first line, causing B to be displayed.
So, why might you want to do this?
* Variables are too pedestrian for you
* To mess with the people writing static analysis tools
* You feel that speed is overrated
* So you'll be able to complain in the future if someone makes a tool that "fixes" the memory leak
* You want a program that will automatically exit after a while to preserve battery life
* Your sto→ button is broken
* To get back at Big Calculator Tutorial for concealing these secrets from you for so long
There is, of course, much room for improvement here - the program above basically uses unary, but I imagine it would be possible to use multiple different loops instead of just one If/Else to store one of several "digits" for each value pushed to the stack. This would allow you to store a massive amount of digits, more than a list can hold and probably more than you can access with the sub( command in a string.
It's well known that if you use Goto to exit a block, you'll leak memory. But have you ever wondered what this memory actually is? The OS keeps track of the blocks that it's in on the operator stack so that it knows what to do when it encounters an End statement. Whenever it sees a loop command, it pushes something to the stack that tells it where it which statement to start over from for the next iteration. When it encounters a Then block that it takes, it pushes something that says that it's a Then statement, so that it doesn't try to loop back anywhere when it hits an End statement. Normally, this works fine for keeping track of blocks, but if you use Goto, you can skip over an End statement, leaving an extra element on the stack. For Then statements, this usually just means that the memory gets leaked, but when you also Goto out of loops, you can do some interesting stuff with it.
For example, here's a program that counts how many iterations of a loop pass between the time that you start the program and the time that you press a key:
Code:
For(A,0,1
If not(A
Goto A
Disp B
Stop
Lbl A
If not(getKey
Then
Goto A
End
0→B
Lbl B
B+1→B
End
Goto B
Notice that at no point during the main loop (the second block) is the iteration count stored in a variable. Instead, it's kept track of using the depth of the stack. The next loop pops Then blocks from the stack until it reaches the For( block from the first line, causing B to be displayed.
So, why might you want to do this?
* Variables are too pedestrian for you
* To mess with the people writing static analysis tools
* You feel that speed is overrated
* So you'll be able to complain in the future if someone makes a tool that "fixes" the memory leak
* You want a program that will automatically exit after a while to preserve battery life
* Your sto→ button is broken
* To get back at Big Calculator Tutorial for concealing these secrets from you for so long
There is, of course, much room for improvement here - the program above basically uses unary, but I imagine it would be possible to use multiple different loops instead of just one If/Else to store one of several "digits" for each value pushed to the stack. This would allow you to store a massive amount of digits, more than a list can hold and probably more than you can access with the sub( command in a string.