How far can stack [LIFO] solve do automatic garbage collection and prevent memory leak ?

Brad hwfwguy at
Tue Aug 17 20:29:36 CEST 2010

On Aug 17, 10:34 am, Standish P <stnd... at> wrote:
> On Aug 16, 11:09 am, Elizabeth D Rather <erat... at> wrote:
> How are these heaps being implemented ? Is there some illustrative
> code or a book showing how to implement these heaps in C for example ?
Forth does not use a heap, except maybe to implement malloc/free which
many Forth apps do not use. The dictionary is a linked list structure.
Now seems like a good time for you to teach yourself Forth (by
studying the best commercial implementation you can find) since you
seem to be working with a clean slate. But I will say a few things
about stacks in general.

There are many ways to implement stacks. The simplest is to declare
some space for the stack and then post-increment or pre-decrement a
stack pointer depending on whether you're pushing or popping. Normally
you make the memory for them big enough that they don't overflow.

If you are concerned about stack overflow you can change the
implementation. Add bounds checking, for example.

Another trick is to use an 8-bit stack pointer. Then you will have a
circular stack. If there is underflow or overflow it at least will not
step on other data. It will only return bad data, which you may find
preferable to an ugly crash. OTOH, bugs that cause spectacular
failures tend to be discovered. You can also initialize the stack
memory with a pattern like 0xDEAD and then after sufficiently
exercising the code, examine the memory contents to see the "high
water mark" of the stack pointer.


More information about the Python-list mailing list