13 January 2016

Lesson 4 – Explaining the implementation of the stack

Further explanation of the .s word

In the previous lesson we saw the .s word in action in order to show the contents of the stack. In reality the stack uses pointers. In the picture you see what happens when dup duplicates an item on the stack.


















When dup copies Item 3 it only copies the pointer to Item 3. So after the dup, both topmost items contain equal pointers to the memory address of Item 3.

How does it look in the 8th REPL?

We’ll now show you the same thing in the 8th REPL.














The columns in each line in the output of .s have this meaning:
1. index of the item. Item at the bottom always has index 1 and so on.
2. class of the item. We’ll explain that later. (n: is numeric)
3. memory address where this stack item points to
4. reference count for this item
5. value of this item

This picture makes it clear that after the dup the 2 topmost items point to the same memory address.

Also after the dup the reference count was incremented from 1 to 2.

Why reference counting?

Reference counting is the means 8th uses to know when an item can be disposed off, namely when the reference count has become zero.

The process to remove such unused stuff is called garbage collection. It save the programmer a lot of manual work.

More info about 8th

There is a central website for 8th here: http://8th-dev.com/

Video

Watch the complementary video here

No comments:

Post a Comment