#Stack
8 messages in this thread
The stack is automatic? Do you mean that there is nothing to set up and
all I have to do is send a number to a certain location? Or do you mean
that the computer will use the stack for me?
Chris,
Yes, the stack is automatic. That's why there's a STACK command so
you can set it's size (or the equivalent in a WB ICON for a program) so you
don't run out of it (with disastrous consequences) while a program is
running.
When a program is compiled, part of the compilation process is that
the necessary code for passing parameters, saving registers, etc. onto the
stack is automatic…in other words, the compiler does it since it's part
of the way languages are designed.
You can write code to directly use the stack yourself, but usually
that's not a good idea. Instead, you create your own private stack
(usually known as a 'heap') and use that as you will.
Oh…each process has it's own stack. Thus if you've got 3 things
running on your Amiga, there's 3 memory areas reserved…one for each
program…as their stacks.
Don
(butting in…)
Sorry, but the heap is NOT the stack!
The stack is an area of memory reserved for return pointer, parameter, and
local variable storage for functions and procedures (as discussed in this
thread).
The heap is basically the rest of system memory. It is memory that is not
otherwise occupied (by program, static data, the operating system, etc),
and is therefore available for dynamic storage allocation.
malloc() calls in C use the heap. Local variables in all modern languages
use the stack.
Hope I didn't muddy the waters. (climbing back under my rock).
Michael B. Haynie Compuserve: 72017,1030
UUCP: attmail!wisdom!guru4!haynie
Michael,
Quite correct…the stack and the heap are not the same, and I
didn't say they were. I said you could create your own private stack and
use it as you will…and I said that private stack was normally called a
heap.
I suppose based on dictionary definition, you're correct. 'Heap'
is the available system memory that can be allocated to a program.
However, I've been taught, and use it to refer to a local pool of memory,
already allocated to your program for "generic" use…such as a private
stack like structure or array that can be used for whatever the programmer
wants. For example, I once wrote a language parser, and used (what I call)
a heap to keep track of nested if/else/endif calls.
Don
sounds like it makes a difference if your heap is allocated out of a 'heap'
which can be shared by other processes
or if it is on a machine where whatever process exists has access to all
the mem by default, if it cares to calloc some of it
Very true. Normally the malloc() library will insulate the programmer from
the difference, but sometimes…..
Mike Haynie
OK, no more hair splitting, I'm just comming from the C angle.
Mike Haynie
I think I understand now. Now some things have come together. If I had
remember that there was "heap stack" I probably would have asked what that
was. Anyhow, thanks for the help on the stack.