stack overflow

This is a discussion on stack overflow within the C Programming forums, part of the General Programming Boards category; Hi, During Recursion, I m getting stack overflow error. Is There any technique to avoid it. Thanks, Raja,...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    7

    Question stack overflow

    Hi,
    During Recursion, I m getting stack overflow error.
    Is There any technique to avoid it.

    Thanks,
    Raja,

  2. #2
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    do not recurse that much

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    1. Don't use recursion.
    2. Make sure your recursive function actually has an end condition which doesn't involve recursion
    3. Use a decent OS/Compiler that doesn't limit the stack to say 2K of memory
    4. If you have got a crippled compiler, read the manual to find out how to maximise whatever stack space you have.
    5. Don't use big local variables - malloc and free them on each recursive instance.
    6. Read this thread - stack overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Salem
    6. Read this thread - stack overflow
    Haha...nice.
    Quote Originally Posted by Salem
    5. Don't use big local variables - malloc and free them on each recursive instance.
    How does this help?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by pianorain
    Haha...nice.
    How does this help?
    Less on the stack means more stack space. A recursive call which has no local variables will allow for more calls due to it taking up less stack space than one with one single character. Now compare that with one that has a huge object:
    Code:
    int counter = 0;
    void recurse1( void )
    {
        printf("counter is %d\n", counter++ );
    }
    
    void recurse2( void )
    {
        char byte;
        printf("counter is %d\n", counter++ );
    }
    
    void recurse3( void )
    {
        char array[ BUFSIZ + BUFSIZ + BUFSIZ + BUFSIZ ];
        printf("counter is %d\n", counter++ );
    }
    Now assuming you could recover from the first stack overflow, you could reset the counter, call the second function, and it would overflow before it reached the count the previous one. Then assuming you could recover again, you could go with the third call, and it would make even less calls before killing your stack.

    The reason again is that you're less on the stack each time, so you can manage to get more calls through before it dies.

    It's like if you're stacking single sheets of paper, versus stacking books, versus stacking boxes of books. Counting singular objects, you'll get a lot more paper stacked than you will boxes before you reach the ceiling of your room.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    > How does this help?
    Code:
    void recursive ( int n ) {
      char big[1000];  // 1000 bytes a pop
    }
    
    void recursive ( int n ) {
      char *big = malloc ( 1000 );  // only sizeof(char*) bytes on the stack
    }
    It's more work (like calling free, and wondering what to do with NULL pointers)

    Meh - a rare victory for Quzah with the busy fingers
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2005
    Location
    Transcarpathia
    Posts
    49
    For every function call stack frame is allocated.
    Shortly, the stack frame consists of return address, stack pointer and local variables.
    If local variables take a lot of space (for example, huge arrays), less stack frames
    (read: recursive function calls ) can be squeezed into available stack space.

  8. #8
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ah, thanks for those explanations, you guys. I didn't realize that local variables were held in the stack frame, but I guess I should have. Now that you mention it, I do remember hearing something to that effect in one of those programming classes.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  2. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21