On 2004-06-30,
<> wrote:
> I am quite puzzled by the stack overflow which I am encountering.Here
> is the pseudocode
This is not a C question. If you need additional help beyond my post,
you should probably consult comp.programming (where follow-ups have been
redirected).
>
> //define stack structure
>
> //function operating on stack
> void my_stack_function( function parameters)
> {
> do required stuff
> if(some conditions obeyed)
> call my_stack_function(function parameters);
> }
>
> //in main()
> {
> initial conditions;
> if(conditions obeyed)
> {
> call my_stack_function(function parameners);
> call a_function_to_pop_contents_of_stack();
>
>
> }
> }
> The conditions are so set that my_stack_function is not called
> infinite number of times. I get a stack overflow when executing this.I
> am not sure if I made any logical error.Does anybody see any silly
> logic??
Even if you limit your recursion depth to a finite number, it is still
possible to run out of memory by calling the function recursively too
many times.
Two things to try are:
(1) turn up the optimizations on your compiler. Your function is
tail recursive and a good optimizing compiler can remove the
recursion for you, or
(2) remove the recursion in your function.
Simplistically, your function can be rewritten without recursion
like this:
void my_stack_function(function parameters)
{
loop:
do required stuff
if(some conditions obeyed)
goto loop;
}
But this assumes that when you call your function recursively,
you are just continuing to pass the same variables that were
passed before (but were modified in the "do required stuff").
-- James