Click here to Skip to main content
15,920,603 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello everyone ,

I am facing One Problem when I am Using Recursion Function

Stack Over Flow Memory using recursion function


Please Give me a reply

Thank You
Posted
Comments
Khaniya 16-Dec-10 7:27am    
show your recursion function code here

Every time the function calls itself, its parameters and local variables are put on the stack. If you call it too many times, you're going to get this error. You're going to have to refactor it. If you post your method, we might be able to make suggestions.
 
Share this answer
 
v2
Well that one is easy enough :) . I can think of two things from the top of my head:

1. The recursive method doesn't have a terminating condition and thus keeps on calling itself until the stack overflows.

2. The method is properly written but the recursion level is so deep that the stack overflows. For instance take the Ackermann function
(Ackermann Function[^]) its depth grows so darn quickly that back in the days when I studied computer science we used values less than four for the parameters.

So it all depends on what function you're executing and with what parameters.

Cheers,


Manfred
 
Share this answer
 
v5
Comments
JF2015 17-Dec-10 0:24am    
Good one.
Manfred Rudolf Bihy 17-Dec-10 0:31am    
Thanks JF2015.
I think you have forgotten a condition to get out of the recursive loop.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 17-Dec-10 0:36am    
What exactly did you think I meant by "The recursive method doesn't have a termination condition"?
recursive functions don't save memory compared to iterative solutions. Even worse they consume stack memory which is relatively scarse.

To fix your memory leak, you can use smart pointers such as boost::shared_ptr. Alternatively, ditch pointers and dynamic memory altogether, and just return your results by value from recurse().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900