Board index » delphi » Stack overflow error?

Stack overflow error?

Hi all.

I have a problem in my program, where after a I run a procedure a few
times, I get a stack overflow error.  All the variables for that
procedure are local variables.  making them global variables helps, but
thats messy.

My question is this:  How can I allocate that stack space
again, in the same variables, without the stack overflow error, or at
the very least, leave those variables local for code neatness, without
this error?

Thanks for your help in advance,

Campbell Basset
v...@aztec.co.za

 

Re:Stack overflow error?


Quote
Campbell Basset <v...@aztec.co.za> wrote:
>Hi all.
>I have a problem in my program, where after a I run a procedure a few
>times, I get a stack overflow error.  All the variables for that
>procedure are local variables.  making them global variables helps, but
>thats messy.

For me Stack overflow means only infinite recursion - I never succeed
to receive it in other way... Is it recursive?

Quote
>My question is this:  How can I allocate that stack space
>again, in the same variables, without the stack overflow error, or at
>the very least, leave those variables local for code neatness, without
>this error?

Didn't get the question... Try to increase stack size...

Quote
>Thanks for your help in advance,
>Campbell Basset
>v...@aztec.co.za

Cheers
  Dmitri

--------------------------------------------------------------
        Shit happens, but why it always happens to us?
--------------------------------------------------------------
Dmitri Poujlivyi            mailto:  dmi...@god.bel.alcatel.be
                            http://www.dma.be/p/bewoner/dmitri

Re:Stack overflow error?


Quote
Campbell Basset wrote:
> I have a problem in my program, where after a I run a procedure a few
> times, I get a stack overflow error.  All the variables for that
> procedure are local variables.  making them global variables helps, but
> thats messy.

> My question is this:  How can I allocate that stack space
> again, in the same variables, without the stack overflow error, or at
> the very least, leave those variables local for code neatness, without
> this error?

Sounds like a recursive call of procedure (i.e. calling the procedure
from
within the procedure itself).
If that's the case, try to restructure the program so that it's _not_
recursive anymore. If this is not acceptable think about using dynamic
variables (do check heap then and remember that any pointer uses 4 bytes
of
memory when pushed onto the stack).

Did you set the stack limit to maximum (see online help for that)?

Hope this helps.

Bernd

Re:Stack overflow error?


Quote
> I have a problem in my program, where after a I run a procedure a few
> times, I get a stack overflow error.  All the variables for that
> procedure are local variables.  making them global variables helps, but
> thats messy.

> My question is this:  How can I allocate that stack space
> again, in the same variables, without the stack overflow error, or at
> the very least, leave those variables local for code neatness, without
> this error?

   To deal with this problem (one which most experienced TP/BP
programmers have had), you need to know what the Stack is and what it's
used for.  The Stack is the (Real) memory area used by the RTL to
save/restore the state of the program during subprogram calls, as well as
allocate all procedure-local data.  Therefore, the depth of subprogram
invocation _and_ the variables declared in all those subprograms will
determine how much os the Stack is used/required.  There is almost no way
to determine how much Stack will be used by a program just by compiling
it, and you really have to know how it's structured and will run.
   The things which can affect Stack usage are (1) recursion/nesting of
subprogram calls, (2) the amounts of Vars declared in the subprograms,
and (3) the number/type of parameters being passed to the subprograms.  
Extreme amounts of any (or all) can cause Stack Overflow.
   Solutions?  Well, look for recursion in your design.  Also, reduce the
local Var data in subprograms as much as possible (e.g. "string[24]"
instead of "string", etc.).  Use Var parameters for large structure
arguments in calls.  LASTLY, increase the Stack parameter in the $M
directive - because that's usually only a temporary fix to what's
probably a design problem...

Re:Stack overflow error?


On Wed, 23 Oct 1996 08:35:26 +0200, Campbell Basset <v...@aztec.co.za>
wrote:

Quote
>Hi all.

>I have a problem in my program, where after a I run a procedure a few
>times, I get a stack overflow error.  All the variables for that
>procedure are local variables.  making them global variables helps, but
>thats messy.

>My question is this:  How can I allocate that stack space
>again, in the same variables, without the stack overflow error, or at
>the very least, leave those variables local for code neatness, without
>this error?

>Thanks for your help in advance,

>Campbell Basset
>v...@aztec.co.za

Dear Campbell,

You can create local 'global' variables by declaring variable CONST's.
Here's an example:

VAR Var1:INTEGER;

PROCEDURE MyRecursiveProcodecure;
CONST Var2:INTEGER=23;
VAR Var3:INTEGER;
BEGIN
  .........
  MyRecursiveProcodecure;  {recurse}
  .........
END;

Variable  Scope   Init. val.   Stack usage?

  Var1    Global      0            No
  Var2    Local       23           No
  Var3    Local    unknown        Yes!

I hope that this will help you.

Peter

PS: next time metion which compiler you are using, and give an
example!

Re:Stack overflow error?


Quote
Campbell Basset <v...@aztec.co.za> wrote:
>Hi all.

>I have a problem in my program, where after a I run a procedure a few
>times, I get a stack overflow error.  All the variables for that
>procedure are local variables.  making them global variables helps, but
>thats messy.

>My question is this:  How can I allocate that stack space
>again, in the same variables, without the stack overflow error, or at
>the very least, leave those variables local for code neatness, without
>this error?

There are several reasons for this error. One possibility is that you
give a large data structure (an array, for example) as value parameter.
That way it will be copied on the stack (normaly only 64 kB, remember).
Solution: Use var-parameters instead, these use only a pointer (4 bytes).

Second: You declare large data structures inside the procedure. They to
land on the stack. A solution is possible in some circumstances by using
constant variables (const VariableName : VariableType = Value;), as those
are located in the code or variable, not the stack segment (The precise
location depends on the TP version). These are, despite their name,
variables (i.e., their value can be changed at run time). There will also
be only one copy of that data structure per program, rather than one per
procedure call (which, depending on application, may be an advantage or a
disadvantage).

Re:Stack overflow error?


Quote
In article <326e7a11.9547...@news.worldonline.nl> Peter de Jong wrote:
>You can create local 'global' variables by declaring variableCONST's.
>Here's an example:

>VAR Var1:INTEGER;

>PROCEDURE MyRecursiveProcodecure;
>CONST Var2:INTEGER=23;
>VAR Var3:INTEGER;
>BEGIN
>  .........
>  MyRecursiveProcodecure;  {recurse}
>  .........
>END;

>Variable  Scope   Init. val.   Stack usage?

>  Var1    Global      0            No
>  Var2    Local       23           No
>  Var3    Local    unknown        Yes!

>I hope that this will help you.

Hello Peter,

By local "global" I assume you mean local static variables.  
I wouldn't been too keen on assuming that var1 was zero.  By
definition it is an uninitialized variable, therefore I would
treat it that way.  

My guess is that version 7.0 has been saving your bacon.  I ran a
short program from the IDE for all versions from 1 through 7 and
only 7.0 initialized the variable to zero before the program was
executed.  Most took the uninitialized contents of memory, a few
initialized the variable when the program was first compiled, but
retained the modified value for the next run.  

I wouldn't vote for using CONST variables to minimize stack usage
either.  Early versions of TP placed such constants in the code
segment which was limited to 64k.  The current versions of TP place
initialized variables in the data segment, which also has a 64k
limit.  Regardless the version, it seems you are simply swapping
one problem for another.  The idea of local variables is that they
don't exist throughout the duration of the program's life, but only
during the period a routine is active.

    ...red

Other Threads