stack-overflow

try/catch on stack overflows in java?

♀尐吖头ヾ 提交于 2019-11-28 02:30:56
问题 Can you try/catch a stack overflow exception in java? It seems to be throwing itself either way. When my procedures overflows, I'd like to "penalize" that value. 回答1: Seems to work: public class Test { public static void main(String[] argv){ try{ main(null); } catch(StackOverflowError e){ System.err.println("ouch!"); } } } 回答2: If you are getting a stack overflow, you are likely attempting infinite recursion or are severely abusing function invocations. Perhaps you might consider making some

Stackoverflow with Quicksort Java implementation

你离开我真会死。 提交于 2019-11-28 01:59:11
Having some problems implementing quicksort in java. I get a stackoverflow error when I run this program and I'm not exactly sure why. If anyone can point out the error, it would be great. si is the starting index. ei is the ending index. public static void qsort(int[] a, int si, int ei){ //base case if(ei<=si || si>=ei){} else{ int pivot = a[si]; int length = ei - si + 1; int i = si+1; int tmp; //partition array for(int j = si+1; j<length; j++){ if(pivot > a[j]){ tmp = a[j]; a[j] = a[i]; a[i] = tmp; i++; } } //put pivot in right position a[si] = a[i-1]; a[i-1] = pivot; //call qsort on right

Python: nested lambdas — `s_push: parser stack overflow Memory Error`

♀尐吖头ヾ 提交于 2019-11-28 01:34:27
问题 I recently stumbled across this article which describes how to code FizzBuzz using only Procs in Ruby, and since I was bored, thought it would be neat to try and implement the same thing in Python using lambdas. I got to the section where you create numbers using nested functions, and wrote the following Python script: #!/usr/bin/env python zero = lambda p : (lambda x: x) one = lambda p : (lambda x: p(x)) two = lambda p : (lambda x: p(p(x))) three = lambda p : (lambda x: p(p(p(x)))) five =

Stack overflow error handling in finally block

此生再无相见时 提交于 2019-11-28 01:27:08
问题 I have a program in java, which runs infinite times. Program code: void asd() { try { //inside try block System.out.println("Inside try !!!"); asd(); } finally { //inside finally System.out.println("Inside finally !!!"); asd(); } } OUTPUT : this program runs infinitely, by constantly printing both the sysouts. My question : At some point, it starts throwing StackOverflowErrors from the try block and so it reaches the finally block, where we are again calling this function recursively. But how

An unhandled exception of type 'System.StackOverflowException' occurred

孤街浪徒 提交于 2019-11-28 00:21:51
问题 Why this? This is my code : public class KPage { public KPage() { this.Titolo = "example"; } public string Titolo { get { return Titolo; } set { Titolo = value; } } } I set data by the constructor. So, I'd like to do somethings like KPage page = new KPage(); Response.Write(page.Titolo); but I get that error on : set { Titolo = value; } 回答1: You have an infinite loop here: public string Titolo { get { return Titolo; } set { Titolo = value; } } The moment you refer to Titolo in your code, the

Why am I getting a StackOverflowError exception in my constructor

余生颓废 提交于 2019-11-28 00:11:23
public class Cloning { Cloning c=new Cloning(); public static void main(String[] args) { Cloning c=new Cloning(); c.print(); } public void print(){ System.out.println("I am in print"); } } In the above code I have a simple class and a class level instance, I also have a local instance with the same name. When running the above code I get below exception : Exception in thread "main" java.lang.StackOverflowError at com.java8.Cloning.<init>(Cloning.java:6) Your main method creates a Cloning instance ( Cloning c=new Cloning(); ), which causes the initialization of the instance variable c ( Cloning

How to handle StackOverflowError in Java?

大憨熊 提交于 2019-11-27 23:33:46
How to handle StackOverflowError in Java ? Huxi I'm not sure what you mean with "handle". You can certainly catch that error: public class Example { public static void endless() { endless(); } public static void main(String args[]) { try { endless(); } catch(StackOverflowError t) { // more general: catch(Error t) // anything: catch(Throwable t) System.out.println("Caught "+t); t.printStackTrace(); } System.out.println("After the error..."); } } but that is most likely a bad idea, unless you know exactly what you are doing. Andrew Bullock You probably have some infinite recursion going on. I.e.

Windows: avoid pushing full x86 context on stack

萝らか妹 提交于 2019-11-27 20:42:21
I have implemented PARLANSE , a language under MS Windows that uses cactus stacks to implement parallel programs. The stack chunks are allocated on a per-function basis and are just the right size to handle local variables, expression temp pushes/pops, and calls to libraries (including stack space for the library routines to work in). Such stack frames can be as small as 32 bytes in practice and often are. This all works great unless the code does something stupid and causes a hardware trap... at which point Windows appears to insist on pushing the entire x86 machine context "on the stack".

Try-finally block prevents StackOverflowError

岁酱吖の 提交于 2019-11-27 19:44:50
问题 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? 回答1: 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

Merge sort from “Programming Scala” causes stack overflow

最后都变了- 提交于 2019-11-27 19:11:08
A direct cut and paste of the following algorithm: def msort[T](less: (T, T) => Boolean) (xs: List[T]): List[T] = { def merge(xs: List[T], ys: List[T]): List[T] = (xs, ys) match { case (Nil, _) => ys case (_, Nil) => xs case (x :: xs1, y :: ys1) => if (less(x, y)) x :: merge(xs1, ys) else y :: merge(xs, ys1) } val n = xs.length / 2 if (n == 0) xs else { val (ys, zs) = xs splitAt n merge(msort(less)(ys), msort(less)(zs)) } } causes a StackOverflowError on 5000 long lists. Is there any way to optimize this so that this doesn't occur? It is doing this because it isn't tail-recursive. You can fix