+ 13
Why this program is not causing stackOverflow Error. I'm Calling main method infinitely?🙄
13 Respostas
+ 2
Lol, weird 😂
+ 10
well i figure it out, I mean we figure it out (Me and my friend she noticed) actually it was my infinite for(;;) loop which was avoiding stackoverflow... I was doing recursion plus infinite for loop and it was the for loop which was iterating all the time.
When i remove the for(;;) for the code and do recursion alone. it causes stackOverflow like say after running 2-3k times.
Still weird though😟
+ 9
@Jay I'm doing infinite recursion too🤔
+ 8
:) but how much memory do you think it will consume. it overwrites what came before it on each iteration does it not?
+ 8
@Jay this program cause stackOverflow after running for over 2k times on my pc.
class StackOverflow{
static int i=0;
public static void main(String[] args){
target();
}
static void target(){
i++;
System.out.println(i+" hey");
target();
}
}
But when i try to call main() method recursively for infinite it just goes on and on and never cause stackoverflow.
Does it means java main method don't cause stackoverflow in recursion? Or I'm wrong?
+ 8
Weird. It is supposed to overflow. We even have almost similar examples.
https://stackoverflow.com/questions/22624983/call-main-recursively
+ 8
Yup... it goes on and on for forever.
when i remove for(;;) then it caused stackOverflowException
+ 6
... its an infinite loop alright. but is it leaking memory?
+ 6
i would have to test this out. but i believe what is happening is the code is hitting int_max and when incrementing with i++ it wraps around to -2million something or other and freaks out.
+ 6
@Garikai i tested it on my pc.
+ 3
I think SoloLearn put a limit to the amount of time a non web program is supposed to run, so I'm guessing the time limit was exceeded before the call stack overflowed.
+ 3
@Fuyuka, oh you mean it's not causing a StackOverFlowException on your PC?
0
If only Java had tail recursion elimination...