stack-overflow

Achieving Stackless recursion in Java 8

非 Y 不嫁゛ 提交于 2019-11-28 17:12:33
问题 How do I achieve stackless recursion in Java? The word that seems to come up the most is "trampolining", and I have no clue what that means. Could someone IN DETAIL explain how to achieve stackless recursion in Java? Also, what is "trampolining"? If you cannot provide either of those, could you please point me in the right direction (i.e., a book to read about it or some tutorial that teaches all of these concepts)? 回答1: A trampoline is a pattern for turning stack-based recursion into an

How to predict the maximum call depth of a recursive method?

故事扮演 提交于 2019-11-28 16:49:05
For the purposes of estimating the maximum call depth a recursive method may achieve with a given amount of memory, what is the (approximate) formula for calculating the memory used before a stack overflow error is likely to occur? Edit: Many have responded with "it depends", which is reasonable, so let's remove some of the variables by using a trivial but concrete example: public static int sumOneToN(int n) { return n < 2 ? 1 : n + sumOneToN(n - 1); } It is easy to show that running this in my Eclipse IDE explodes for n just under 1000 (surprisingly low to me). Could this call depth limit

Why does this method print 4?

随声附和 提交于 2019-11-28 15:33:23
I was wondering what happens when you try to catch an StackOverflowError and came up with the following method: class RandomNumberGenerator { static int cnt = 0; public static void main(String[] args) { try { main(args); } catch (StackOverflowError ignore) { System.out.println(cnt++); } } } Now my question: Why does this method print '4'? I thought maybe it was because System.out.println() needs 3 segments on the call stack, but I don't know where the number 3 comes from. When you look at the source code (and bytecode) of System.out.println() , it normally would lead to far more method

Why does a recursive call cause StackOverflow at different stack depths?

拥有回忆 提交于 2019-11-28 15:30:09
问题 I was trying to figure out hands-on how tail calls are handled by the C# compiler. (Answer: They're not. But the 64bit JIT(s) WILL do TCE (tail call elimination). Restrictions apply.) So I wrote a small test using a recursive call that prints how many times it gets called before the StackOverflowException kills the process. class Program { static void Main(string[] args) { Rec(); } static int sz = 0; static Random r = new Random(); static void Rec() { sz++; //uncomment for faster, more

Try-finally block prevents StackOverflowError

感情迁移 提交于 2019-11-28 14:57:06
Take a look at the following two methods: public static void foo() { try { foo(); } finally { foo(); } } public static void bar() { bar(); } Running bar() clearly results in a StackOverflowError , but running foo() does not (the program just seems to run indefinitely). Why is that? It doesn't run forever. Each stack overflow causes the code to move to the finally block. The problem is that it will take a really, really long time. The order of time is O(2^N) where N is the maximum stack depth. Imagine the maximum depth is 5 foo() calls foo() calls foo() calls foo() calls foo() which fails to

Unusual stack overflow when inserting nodes in binary tree

寵の児 提交于 2019-11-28 14:28:34
CLISP Version: 2.49 Leaf Node (value (NIL) (NIL)) Non-Leaf Node (value (value (NIL) (NIL)) (NIL)) Code ("format" for debug only) ; (nil) means NULL (defun binary-insert (root obj <) (if (null (cdr root)) (progn (format t "In Null [~A] => " root) (setf (car root) obj) (format t "mid [~A] => " root) (setf (cdr root) '((nil) (nil))) (format t "[~A]~%" root)) (if (funcall < obj (car root)) (progn (format t "In Left [~A] => " root) (binary-insert (nth 1 root) obj <) (format t "[~A]~%" root)) ; Left (progn (format t "In Right [~A] => " root) (binary-insert (nth 2 root) obj <) (format t "[~A]~%" root

Create objects without causing a stack overflow error?

淺唱寂寞╮ 提交于 2019-11-28 14:28:33
So, I have my main class that calls private Secondary secondary = new Secondary(); when it runs. In the Secondary class, at the top I have code that says private Main main = new Main(); . How will I be able to use all of the methods and variables from the Secondary class and vice versa without causing a stack overflow error? Note: they are not in the constructor Your Main class is creating a Secondary instance, which is creating a Main instance..., and this is causing the stack overflow error. I think you just want the objects to refer to each other, so don't create the other class's new

How can I avoid a stack overflow when Fortran produces a large, internal, temporary array?

二次信任 提交于 2019-11-28 14:13:36
I have some Fortran code that calls RESHAPE to reorder a matrix such that the dimension that I am now about to loop over becomes the first varying dimension (Column-major order in Fortran). This has nothing to do with C/Fortran interoperability. Now the matrix is rather large and when I call the RESHAPE function I get a seg fault which I am very confident is a stack overflow. I know this because I can compile my code in ifort with -heap-arrays and the problem disappears. I do not want to modify the stack-size. This code needs to be portable for any computer without the user having to concern

Large VLA overflow

瘦欲@ 提交于 2019-11-28 14:02:36
Based on a comment of someone in another thread: VLAs introduce more problems than they solve, because you never know if the declaration is going to crash for x being too large for the stack. This code will overflow because sizeof(a) is too long for the stack: #include <stdio.h> #include <stdlib.h> int main(void) { int n = 100000000; int a[4][n]; printf("%zu\n", sizeof(a)); return 0; } But this one can not because sizeof(a) is 8 (the size of a pointer in my computer): #include <stdio.h> #include <stdlib.h> int main(void) { int n = 100000000; int (*a)[n]; printf("%zu\n", sizeof(a)); a = malloc

Fatal Python error: Cannot recover from stack overflow. During Flood Fill

跟風遠走 提交于 2019-11-28 13:37:15
I've come to a dead end, and after excessive (and unsuccessful) Googling, I need help. I'm building a simple PyQt4 Widget where it lies out a grid of 60x80 squares, each initialized to None . If the user clicks on that box it changes color based on how many times left-clicked, defined by this list: self.COLORS=[ (0, 0, 255), #WATER (255, 210, 128), #SAND (0, 128, 0), #GREEN (255, 255, 0), #YELLOW (255, 165, 0), #ORANGE (255, 0, 0) #RED ] If the user right clicks, it flood fills an area, using the common recursive flood fill algo. This works perfectly for small spaces, however if the space is