[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stack vs. heap?



Martin Doherty wrote:
> I believe I thoroughly understand the operation of a stack, but the term
> "heap" is a mystery.

A heap isn't a classical data structure in the way a stack or queue is,
so don't get confused by the similarity in naming.

> I know it's used in UCSD/Apple Pascal for memory
> management functions of some kind; can anyone explain to me what a heap
> is in general concept, and what it's used for in Pascal?

A heap is a chunk of memory that is used by a language to keep track of
variables.  Applesoft, for example, stores variables at HIMEM and works
its way "down" in memory as variables get filled/used.  That's
conceptually a heap, though I don't think it ever gets refered to that
way.

Other languages have similar constructs - a procedure in Pascal has
variables declared up front; those get thrown on to the heap, wherever
it resides, when the proceudre is entered.  On exit, that heap space is
freed (becuase it goes out of scope).  Recursive calls to that
procuedure each get their own heap space allocated/deallocated as they
go into and out of scope.

So, you can see that a heap could be easily implemented as a stack. :-)