Why this program is not causing stackOverflow Error. I'm Calling main method infinitely?🙄 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 13

Why this program is not causing stackOverflow Error. I'm Calling main method infinitely?🙄

https://code.sololearn.com/cxUFUilkQs9l/?ref=app

15th Jul 2017, 8:48 AM
Fuyuka Ayumi(冬花)
13 Answers
+ 2
Lol, weird 😂
15th Jul 2017, 7:14 PM
Garikai
Garikai - avatar
+ 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😟
15th Jul 2017, 10:27 AM
Fuyuka Ayumi(冬花)
+ 9
@Jay I'm doing infinite recursion too🤔
15th Jul 2017, 8:54 AM
Fuyuka Ayumi(冬花)
+ 8
:) but how much memory do you think it will consume. it overwrites what came before it on each iteration does it not?
15th Jul 2017, 8:56 AM
jay
jay - avatar
+ 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?
15th Jul 2017, 9:30 AM
Fuyuka Ayumi(冬花)
+ 8
Weird. It is supposed to overflow. We even have almost similar examples. https://stackoverflow.com/questions/22624983/call-main-recursively
15th Jul 2017, 9:57 AM
Hatsy Rei
Hatsy Rei - avatar
+ 8
Yup... it goes on and on for forever. when i remove for(;;) then it caused stackOverflowException
15th Jul 2017, 7:09 PM
Fuyuka Ayumi(冬花)
+ 6
... its an infinite loop alright. but is it leaking memory?
15th Jul 2017, 8:53 AM
jay
jay - avatar
+ 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.
15th Jul 2017, 10:00 AM
jay
jay - avatar
+ 6
@Garikai i tested it on my pc.
15th Jul 2017, 7:04 PM
Fuyuka Ayumi(冬花)
+ 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.
15th Jul 2017, 7:03 PM
Garikai
Garikai - avatar
+ 3
@Fuyuka, oh you mean it's not causing a StackOverFlowException on your PC?
15th Jul 2017, 7:08 PM
Garikai
Garikai - avatar
0
If only Java had tail recursion elimination...
15th Jul 2017, 5:27 PM
Igor B
Igor B - avatar