Board index » delphi » Stack Overflow Error

Stack Overflow Error

        What does this error message means?


Andre Paim Lemos - Estudante de Ciencia da Computacao na UFMG
Fone : (031) 484-2621


Re:Stack Overflow Error

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.


   Procedure Kissa(koira: integer);


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
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 ## **/

Re:Stack Overflow Error

>    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
   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

Other Threads