Stack overflow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ip4ram@yahoo.com

    Stack overflow

    I am quite puzzled by the stack overflow which I am encountering.He re
    is the pseudocode

    //define stack structure

    //function operating on stack
    void my_stack_functi on( function parameters)
    {
    do required stuff
    if(some conditions obeyed)
    call my_stack_functi on(function parameters);
    }

    //in main()
    {
    initial conditions;
    if(conditions obeyed)
    {
    call my_stack_functi on(function parameners);
    call a_function_to_p op_contents_of_ stack();


    }
    }
    The conditions are so set that my_stack_functi on 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??

    Thanks for your help
    Ram
  • James Hu

    #2
    Re: Stack overflow

    On 2004-06-30, ip4ram@yahoo.co m <ip4ram@yahoo.c om> wrote:[color=blue]
    > I am quite puzzled by the stack overflow which I am encountering.He re
    > is the pseudocode[/color]

    This is not a C question. If you need additional help beyond my post,
    you should probably consult comp.programmin g (where follow-ups have been
    redirected).
    [color=blue]
    >
    > //define stack structure
    >
    > //function operating on stack
    > void my_stack_functi on( function parameters)
    > {
    > do required stuff
    > if(some conditions obeyed)
    > call my_stack_functi on(function parameters);
    > }
    >
    > //in main()
    > {
    > initial conditions;
    > if(conditions obeyed)
    > {
    > call my_stack_functi on(function parameners);
    > call a_function_to_p op_contents_of_ stack();
    >
    >
    > }
    > }
    > The conditions are so set that my_stack_functi on 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??[/color]

    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_functi on(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

    Comment

    • Richard Bos

      #3
      Re: Stack overflow

      ip4ram@yahoo.co m wrote:
      [color=blue]
      > //define stack structure
      >
      > //function operating on stack
      > void my_stack_functi on( function parameters)
      > {
      > do required stuff
      > if(some conditions obeyed)
      > call my_stack_functi on(function parameters);
      > }
      >
      > //in main()
      > {
      > initial conditions;
      > if(conditions obeyed)
      > {
      > call my_stack_functi on(function parameners);
      > call a_function_to_p op_contents_of_ stack();
      > }
      > }
      > The conditions are so set that my_stack_functi on 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??[/color]

      Why, yes. The silly logic is in thinking that your code can be analysed
      for silly errors when you don't even provide your code. The error is
      most likely to be in your conditions or your stack definition; why did
      you not post them?

      Richard

      Comment

      • ip4ram@yahoo.com

        #4
        Re: Stack overflow

        Hi Richard,

        I know I am doing injustice to the groups by asking them for help,but
        not posting the code.The pseudocode ,which I have posted here, is a
        part of much larger and very complex image processing
        algorithm.Howev er,if you are still interested in the code,I can mail
        the relevant portion to you.

        Ram
        rlb@hoekstra-uitgeverij.nl (Richard Bos) wrote in message news:<40e264a1. 496689696@news. individual.net> ...[color=blue]
        > ip4ram@yahoo.co m wrote:
        >[color=green]
        > > //define stack structure
        > >
        > > //function operating on stack
        > > void my_stack_functi on( function parameters)
        > > {
        > > do required stuff
        > > if(some conditions obeyed)
        > > call my_stack_functi on(function parameters);
        > > }
        > >
        > > //in main()
        > > {
        > > initial conditions;
        > > if(conditions obeyed)
        > > {
        > > call my_stack_functi on(function parameners);
        > > call a_function_to_p op_contents_of_ stack();
        > > }
        > > }
        > > The conditions are so set that my_stack_functi on 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??[/color]
        >
        > Why, yes. The silly logic is in thinking that your code can be analysed
        > for silly errors when you don't even provide your code. The error is
        > most likely to be in your conditions or your stack definition; why did
        > you not post them?
        >
        > Richard[/color]

        Comment

        • Malcolm

          #5
          Re: Stack overflow


          <ip4ram@yahoo.c om> wrote[color=blue]
          >
          > I am quite puzzled by the stack overflow which I am encountering.He re
          > is the pseudocode
          >
          > //define stack structure
          >
          > //function operating on stack
          > void my_stack_functi on( function parameters)
          > {
          > do required stuff
          > if(some conditions obeyed)
          > call my_stack_functi on(function parameters);
          > }
          >
          > //in main()
          > {
          > initial conditions;
          > if(conditions obeyed)
          > {
          > call my_stack_functi on(function parameners);
          > call a_function_to_p op_contents_of_ stack();
          > }
          > }
          > The conditions are so set that my_stack_functi on 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??
          >[/color]
          We can only guess from pseudo-code. The obvious candidate is that you call a
          function to pop the stack after the recursvie calls have terminated. However
          in a typical recursive system you should be popping the stack each time the
          recursive function yields control.


          Comment

          • Peter Ammon

            #6
            Re: Stack overflow

            ip4ram@yahoo.co m wrote:
            [color=blue]
            > I am quite puzzled by the stack overflow which I am encountering.He re
            > is the pseudocode
            >
            > //define stack structure
            >
            > //function operating on stack
            > void my_stack_functi on( function parameters)
            > {
            > do required stuff
            > if(some conditions obeyed)
            > call my_stack_functi on(function parameters);
            > }
            >
            > //in main()
            > {
            > initial conditions;
            > if(conditions obeyed)
            > {
            > call my_stack_functi on(function parameners);
            > call a_function_to_p op_contents_of_ stack();
            >
            >
            > }
            > }
            > The conditions are so set that my_stack_functi on 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??
            >
            > Thanks for your help
            > Ram[/color]

            This answer is OT in C, but some OSes (Unixes in particular) artficially
            limit the stack size. You can usually turn this off with e.g. the
            ulimit command. Try that.

            --
            Pull out a splinter to reply.

            Comment

            Working...