Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / May 2005

Tip: Looking for answers? Try searching our database.

recursive leads to stack overflow

Thread view: 
hyena - 24 May 2005 10:05 GMT
I have a recursive function to buid up a tree. When the tree grwos too big,
my java give me a error of stack overflow. si there some solutions rather
then reconsidering the algorithms?

Thanks!
Thomas Weidenfeller - 24 May 2005 12:17 GMT
> I have a recursive function to buid up a tree. When the tree grwos too big,
> my java give me a error of stack overflow.

Use a better algorithm.

> si there some solutions rather
> then reconsidering the algorithms?

If you really want to waste more memory, read the fine manual:

http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/java.html#nonstandard

/Thomas

Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq

hyena - 24 May 2005 15:14 GMT
Thanks!

Seems I have to take a careful look at my iterating alogrithm then.:(

> > I have a recursive function to buid up a tree. When the tree grwos too big,
> > my java give me a error of stack overflow.
[quoted text clipped - 9 lines]
>
> /Thomas
mkrause - 24 May 2005 16:50 GMT
Sigh...  *really* helpful Mr Weidenfeller.

If you are getting a stack overflow error, this is NOT because you are
running out of memory.  It is because your recursive algorithm does not
properly end its recursion.  I would check your logic to see how you
determine when you are done.  Typically the pseudocode will be
something like:

Recursive_function(Object current node)
{
 // Do pre-recursive stuff here

 If (current node is not a leaf node)
 {
   Recursive_function(current node);
 }

 // Do post-recursive stuff here
}
mitch - 24 May 2005 19:40 GMT
To overflow the stack you probably have a LOT of recursive calls to
your method.  Don't think tens, think thousands or more.

A typical reason, other than a simple programming logic error, is a
data circularity.  Suppose you are trying to display a tree of data
elements, where A refers to B and B refers to C and C refers to A.
Each time an element makes a reference you are going one level deeper
in the tree.  Guess what?  Your program will be drawing a tree that
looks like

A
 B
   C
     A
       B

etc.  If your tree algorithm is data dependent you have to be careful
to check for circularities in the data, otherwise you'll end up with
an infinite recursion and a stack overflow.

Hope that helps,

- Mitch Gart


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.