Блог пользователя idk321

Автор idk321, история, 4 года назад, По-английски

If I run a recursive (Java) program on my machine, stack overflow usually occures before reaching depth of 10000 (there is no tail recursion since Java does not make it available unfortunately). Yet on Codeforces my recursive programs on trees work up to 100000 or more (one such program I found is https://codeforces.me/contest/1406/submission/92624199). Does anyone have an idea why is it like this and how can I make it so this would also work on my machine or on other less advanced online judges?

  • Проголосовать: нравится
  • +30
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +1 Проголосовать: не нравится

You can increase a stack size with -Xss flag, so you should you run your program like java -Xss500m Main. You can find more in this question on StackOverflow

»
4 года назад, # |
Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

I think there's stack overflow in codeforces too. I don't remember exactly which problem was that, but I have faced issues when I try to run a recursive function that can go deep upto 10^6 levels.

Edit : Nevermind, I just rechecked all my MLE submissions and all of them were stupid issues not related to stack-overflow. :P

»
4 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

It is possible to get stack overflow 92174392.

Consider adding something like

	public static void solve() {
		//code here
	}
	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(null, () -> solve(), "", 1<<28);
		thread.start();
		thread.join();
	}

which sets the stack size to 1<<28 ($$$2^{28}$$$). Note that this doesn't allow you to do recursion up to $$$2^{28}$$$ levels (you'll get MLE), but it basically overcomes stack overflow. Locally, add the -Xss flag when running. -Xss 512m is usually enough.