Hi,
During Recursion, I m getting stack overflow error.
Is There any technique to avoid it.
Thanks,
Raja,
Hi,
During Recursion, I m getting stack overflow error.
Is There any technique to avoid it.
Thanks,
Raja,
do not recurse that much
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 - http://cboard.cprogramming.com/showthread.php?t=67943
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.
Haha...nice.Originally Posted by Salem
How does this help?Originally Posted by Salem
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
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:Originally Posted by pianorain
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.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++ ); }
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.
> How does this help?
It's more work (like calling free, and wondering what to do with NULL pointers)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 }
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.
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.
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