Board index » delphi » Stack Overflow Error

Stack Overflow Error

        What does this error message means?

[]'s                            

------------------------------------------------------------------------------
Andre Paim Lemos - Estudante de Ciencia da Computacao na UFMG
a...@dcc.ufmg.br  
an...@mastermail.com.br
Fone : (031) 484-2621

 

Re:Stack Overflow Error


Quote
On Thu, 8 Jul 1999 17:32:23 -0300, Andre Paim Lemos wrote:
>    What does this error message means?
..
> []'s

?

Stack overflow error
   means that the stack overflowed.

Example:

   Procedure Kissa(koira: integer);
   Begin
     Kissa(1);
   End;

   Begin
     Kissa(0)
   End.

This overflows the stack, because when Kissa is called the first time,
number 0 is pushed to the stack.
Then the address of the next instruction (End) is pushed to the stack.
Then it jumps to Kissa-routine.
Next, it pushes 1 to the stack.
Then it pushes the address of next instruction (end of Kissa) to the
stack.
Then it jumps to the beginning of Kissa-routine.
And so on.
Not counting here the space used by the frame pointer (BP).

This neverending loop pushes data all the time to the "stack", which
has space for 16kB, perhaps only 1024 bytes, or even 65520 bytes,
depending on the {$M ...} -definition.

The stack has limited space.

Not describing the "stack technology"
more here, I hope you got my point.

Local variables, function calls, parameters, etc all use the
stack space. The stack space is freed when function returns.

--
main(C,f,s){for(/** gcc sig.c;a.out>/dev/audio ## http://iki.fi/bisqwit/ **/
write(C=0,"(C)Bisqwit\n",11);f=99;++C){for(s="+%7%+%7%5%4%2%457%0%0%754%2%+"
"%%%5%542%457%0%0%042%2#+%!#0%+%$%%%"[C&63];s--;f=f*89/84);for(s=999+99*(C&2
);--s;putchar(((s*(f-776?f:0)&32767)*s/199999+39-(C&2?rand()%s/24:0))/16));}}

Re:Stack Overflow Error


Quote
>    What does this error message means?

   It means that something in your code or execution logic caused the
program Stack to overflow.  That means that for the accumulated calls to
subprograms at some point in the program's execution (either nested or
recursive), have put more "state" and parameters data on the Stack than
has been reserved in your code - via the $M directive.
   The Stack is used to save the state of each call to a subprogram, as
well as store the parameters being passed to it, as well as provide
storage for all the procedure-local data declared in the subprogram.  The
program has a fixed amount of Stack with which to work, and that's
declared in the $M directive - either the 16K default or up to 64K
(bytes).  Each time a call is made, space for parameter arguments and the
callee's local data is used, and this process accumulates through the
levels of calls made - the Stack data used is released when the called
subprograms return to their caller.
   Let's say you are using the default of 16K, but you have a subprogram
which declares 18K of local data: you'll get a Stack Overflow as soon as
you call the routine.  This is the simplest case...
   Perhaps you have 16K and 2 routines (A an B) which each declare 10K of
local data: you'll get Stack Overflow when the program calls A and A
calls B.  (Next simplest...)
   If a subprogram is recursive, and it uses 1000 bytes of local data, it
can easily get Stack Overflow when the level of recursive calls exceeds,
say, 15 - it's hard to estimate with recursive subprograms.  It's always
best to use as little procedure-local data declarations as possible in
recursive routines.
   In complex/convoluted logic execution, the heavy nesting of subprogram
calls can build up the usage of the Stack, and it's possible to overflow
the Stack at some point.  This is quite difficult to find and correct in
many cases.
   Solutions?  All depend on your logic, programming style, and the
complexity of your application.  First, you might try increasing the
Stack parameter in the $M directive - this might work, but chances are
good the Stack Overflow abort will occur later, as the program gets more
complex and processes more/different data.  This "quick fix" is not a
good solution.
   You should reduce the procedure-local data declarations in your
subprograms as much as possible (it's probably the root cause of your
problem here) - by using global data (I know, most teachers consider this
blasphemy...) and/or pointers/Heap.
   Consider using Var parameters (reference parameters), instead of pass-
by-value parameters, for large structures being passed to subprograms.  
Keep in mind that value parameter data is _copied_ to the Stack, so large
variables/arrays will consume a lot.  Var (reference) parameters only
place a pointer to the actual data on the Stack, saving a lot of space
there.
   Don't use recursion if you don't know much about this technique or the
data being processed.
   Cut down on the hierarchy of subprograms (and their nesting) in your
design/implementation.

Other Threads