Stack or heap? What's that?

Steve Horne stephenwantshornenospam100 at aol.com
Sun Jan 14 16:51:54 PST 2007


On Fri, 12 Jan 2007 18:34:41 +0000 (UTC), orgoton
<orgoton at mindless.com> wrote:

>One difference between C and D is that data is allocated on the heap instead
>of the stack or something like that. What does that mean?

The word 'stack' can refer to any data structure that works in a
last-in-first-out fashion. 'The stack' normally refers to a special
stack used by the processor. When you call a procedure or a function,
the stack is used to remember where to return to when that procedure
or function is finished. It is also commonly used to store the local
variables for each procedure or function that is currently executing.

Memory that is requested when needed and freed when not needed
(generally independently of procedure calls and returns) comes from
the heap, which is a data structure designed to handle random-order
allocation and freeing of chunks of memory. This is not the only
meaning of the word 'heap' - the word also refers to a data structure
used to efficiently handle priority queues.

In C, memory is allocated on the heap using malloc and free. In C++ it
is allocated on the heap using new and delete.

The stack is very convenient and efficient. For example, it is easy
for the processor to find particular variables on the stack - they are
always at a fixed position (determined by the compiler) relative to
the stack pointer. Only global variables are more convenient and
efficient (since they are always at exactly the same location).

The heap is more problematic since effort is need to keep track of
where in memory each heap-allocated object is stored. Heap allocated
objects can only be accessed via pointers/references (which may be
stored on the stack or in a global variable, or in another
heap-allocated objects). However, heap allocated memory is much more
flexible.

In D (and Java, C# etc), things are slightly complicated by the
garbage collection and the use of reference semantics. The basic
result is that a local variable that is a class instance will be
allocated on the heap, unlike in C++ where it would be allocated on
the stack. D does, however, hold a reference to the object on the
stack. This means that when the function exits, only the reference is
automatically destroyed. The object itself may still exist for some
time, and will not be destroyed at all if some other reference to it
exists. When the garbage collector determines that no further
references to it exist, it will be cleaned up and the memory for the
object will be returned to the heap.

Some of the above statements are actually technically inaccurate in
modern PCs, because of the way modern computers manage memory, but
they do represent the applications view of memory quite accurately.

Stewart Gordon has already listed the different types that are
allocated from the heap even when used as local variables (since they
are reference types) - I just thought I'd add some more explanation.

-- 
Remove 'wants' and 'nospam' from e-mail.



More information about the Digitalmars-d mailing list