> One of the things I find hard to understand is "stack space".  I have read
> cautions to avoid loading up the stack because of it's relatively limited
> supply.  Below are several declarations.  Can someone explain which ones use
> up stack space, and if there is a rule of thumb I should follow in
> determining which declarations do so.  My understanding is that anything
> that is declared as a pointer (ex. s2) would get stored at the high end of
> the dynamic heap, and not in the stack.  Please help me out, I'd really
> appreciate it.
>
    All of your automatics (those declared at the top of a function or
statement block, parameters, etc) are stored on the stack (which is really
just a block on the heap, but that's another issue).  Anything allocated
with MemHandleNew, MemPtrNew, etc, are allocated on the dynamic heap.  If I
recall correctly, fixed blocks (allocated by MemPtrNew) are allocated from
one end of the heap, and relocatable blocks (allocated by MemHandleNew) are
allocated from the other end of the heap.  Dynamic heap space is also scarce
- long term data belongs on the storage heap(s) (use the database APIs).

> char s1[]= "Hello";   // array of characters
> CharPtr  s2;      // pointer to a string
> char s3[20];    // array of 20 caharacters
> CharPtr s4[10];    // array of 10 pointers
> int i;    // integer
> int j[10];    // array of 10 integers
>
> s2 = "Goodbye";
> i = 15;
>

      In your example, s, s2, s3, s4, i, and j are all on the stack (total
usage 92 bytes, assuming ints are 2 bytes), and where that constant string
lives depends on your compiler settings.  I'll let the compiler jockeys
figure out where that one goes - it's probably in your global data, which I
think is on the heap, but don't quote me.  If you wanted to allocate one of
your arrays on the heap, you'd declare it as a pointer and use MemPtrNew to
allocate it.



--
Dan Rowley
Innovative Computer Solutions
Developers of fine software for Newton, Windows CE, Palm Computing Platform,
Windows, and MacOS

Reply via email to