Avoid Stack Overflow Error
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and how to avoid stack overflow error in java policies of this site About Us Learn more about Stack Overflow the
How To Avoid Stack Overflow In Recursion Java
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags how to avoid stack overflow exception in c# Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only
Fix Stack Overflow Error
takes a minute: Sign up How does a “stack overflow” occur and how do you prevent it? up vote 26 down vote favorite 9 How does a stack overflow occur and what are the best ways to make sure it doesn't happen, or ways to prevent one, particularly on web servers, but other examples would be interesting as well? memory stack-overflow stack overflow error windows xp share|improve this question edited Jul 29 at 17:46 Kapol 4,2572727 asked Aug 25 '08 at 14:49 JasonMichael 88231621 add a comment| 9 Answers 9 active oldest votes up vote 42 down vote accepted Stack A stack, in this context, is the last in, first out buffer you place data while your program runs. Last in, first out (LIFO) means that the last thing you put in is always the first thing you get back out - if you push 2 items on the stack, 'A' and then 'B', then the first thing you pop off the stack will be 'B', and the next thing is 'A'. When you call a function in your code, the next instruction after the function call is stored on the stack, and any storage space that might be overwritten by the function call. The function you call might use up more stack for its own local variables. When it's done, it frees up the local variable stack space it used, then returns to the previous function. Stack overflow A stack overflow is when you've used up
here for a quick overview of the site Help Center Detailed answers to any questions you might have
Stack Overflow Error Windows 7
Meta Discuss the workings and policies of this site About Us
Stack Overflow Javascript Error
Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads stack overflow line error with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, http://stackoverflow.com/questions/26158/how-does-a-stack-overflow-occur-and-how-do-you-prevent-it just like you, helping each other. Join them; it only takes a minute: Sign up Java: How to avoid StackOverflowException up vote 10 down vote favorite 2 I'm writing a function that will call itself up to about 5000 times. Ofcourse, I get a StackOverflowException. Is there any way that I can rewrite this code in http://stackoverflow.com/questions/10073471/java-how-to-avoid-stackoverflowexception a fairly simple way?: void checkBlocks(Block b, int amm) { //Stuff that might issue a return call Block blockDown = (Block) b.getRelative(BlockFace.DOWN); if (condition) checkBlocks(blockDown, amm); Block blockUp = (Block) b.getRelative(BlockFace.UP); if (condition) checkBlocks(blockUp, amm); //Same code 4 more times for each side } By the way, what is the limitation of how deep we may call the functions? Thanks! java stack-overflow share|improve this question asked Apr 9 '12 at 12:52 Henrik Karlsson 3,59321534 stackoverflow.com/questions/214741/… –Balaswamy Vaddeman Apr 9 '12 at 13:01 add a comment| 6 Answers 6 active oldest votes up vote 13 down vote accepted Maybe using a stack? void checkBlocks(Block b, int amm) { Stack
This Site Careers Other all forums Forum: Beginning Java How to avoid stack overflow error when using recursion? Louis Lewis Ranch Hand Posts: 86 posted 3 years ago I'm trying to write a chess program with the netbeans GUI creator. So far I've got the visual part of the program down; I've created https://coderanch.com/t/618521/java/java/avoid-stack-overflow-error-recursion a chess board that will set up on command, and with pieces that you can move anywhere on the board by clicking them twcie (once to select, once to move), and now I'm trying to add restrictions to only allow legal moves. In order to do this, I created a separate class, which I'm calling Referee, with several methods, all of which build on each other to finally determine whether or not a given move is legal (for example, one method checks if a move stack overflow is legal only in terms of how different pieces are allowed to move, another determines, given a certain position, and using the first method, if either player is in check, and then a final method uses both the surface review based on move capabilites and the position review based on check to decide once and for all if a move is legal). Anyway, the details aren't super important, but the main point is that there are several methods within this class, all of which build stack overflow error on each other, and thus need to be called within the class itself. However, the class also has one method that I need to call from the main class (the full review, to determine if a move is legal). In order to be able to call methods from the Referee class within the main class, and the Referee class, I created a separate object in each class (ref1 inside Referee class, and ref2 inside the main class). However, I'm now having trouble with a stack overflow error. The error comes up on the object creation in the main class; I guess what's happening is that when I create the ref2 object, it has to have its own ref1 object, which in turn has to have its own ref1 object, etc. etc., on to infinity. public static void main(String args[]) { Referee ref2 = new Referee(); //the stack overflow error is happening here } public class Referee { Referee ref1 = new Referee(); } Does anyone know how to redesign my class set up such that I don't run into this error? I can't figure out a way to call the referee methods from within the referee class without causing this error, although I guess that it is possible since I have seen recursion sucessfully done before. Help would be much appreciated. Maneesh Godbole Saloon Keeper Posts: 11196 15 I like... posted 3 years ago The root cause is Referee ref1 = new Referee(); So why are you constructing a new Referee inst