• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Looping with Main  RSS feed

 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a small program that creates a loop that only fails to continue once we reach a StackOverflowError exception. It looks just like:



The idea of the program is to go between each method without using an object. Each time the method is entered the static int counter variable is incremented and outputted along with its respective loop. The loop does, however, fail eventually.
By calling the between the methods we build a tower of stack frames that eventually topples. The number of times this runs before the StackOverflowError occurs varies, though. Sometimes I get 6553, 6554 or something else close to these values.
Is there a way to pop a method of the stack or clear part of the stack so I can keep this going? I don't know where I would use this but it would be nice to know.

Thanks.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Skye,

I haven't tried it, but what about:


Greetz,
Piet
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Is there a way to pop a method of the stack or clear part of the stack so I can keep this going? I don't know where I would use this but it would be nice to know.

No.

Your program has an infinite recursive loop. main() calls roundAbout() which calls main() which calls roundAbout() which calls main() which calls roundAbout() ... etc. until the stack overflows. Whenever you call a method, the CPU stores some information on the call stack, such as the address in memory where the CPU has to return when the method returns. The stack has a finite size (normally a few MB). When you nest method calls too deeply, the stack will eventually be entirely used up and you get a StackOverflowError. There is no way to delete (part of) the information on the call stack. That would not be good, because then the CPU wouldn't know anymore where to go when a method returns.

you should not normally write programs which have very deep or even infinite recursive loops like this. Use a normal iterative loop (a for loop or while loop) instead of recursive loops.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Is there a way to pop a method of the stack or clear part of the stack so I can keep this going? I don't know where I would use this but it would be nice to know.

Not easily the way you've done it, and not explicitly; but in a "normal" recursive situation - ie, a method that calls itself - that "pop" happens whenever the method returns without calling itself again. Indeed, it's precisely what recursive algorithms rely on.

The normal pattern is that the method calls itself, and the stack builds up, until some condition is reached, when the method returns. This pops the last method off the stack frame and execution proceeds from the point where it was called until another condition (almost always the same one) is met, so that method is popped of the stack... and so on, until eventually you get back to the call that started the whole ball rolling.

It should probably also be pointed out that:

1. As you've seen, stack space is limited - in your case to a few thousand calls - so whatever you're doing shouldn't come even close to requiring that many "levels". This is one of the reasons that recursion is not much good for "linear" algorithms. Quicksort, on the other hand - possibly the most classical recursive algorithm there is - works in powers of two (sometimes called a "divide and conquer" strategy), so in theory, a Quicksort on your machine could sort 2^6000 records (a VERY big number; and believe me, you'll never reach it ).

2. You can increase the available stack size with the -Xss parameter when you invoke your program with the java command, which might be fun to try while you're just "mucking about". In practise, it's rarely a good solution though.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are doing is recursion without a base case. Remember any recursion needs a base case to stop itYou cannot pass a null or a negative value, but when you get to zero, you terminate your recursion. Last time I tried that I ran out of memory about 5000! (that bang sign means factorial not exclamation), so that is comparable to the stack size you are using.

Challenge: write a program to print the last 10000 digits of 1000000!
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:(...)
Challenge: write a program to print the last 10000 digits of 1000000!

Don't!
If you write out '1_000_000!', then for each number, think of whether it contains a
2 or a 5. Then think what that has to do with some number ending on a zero.
Well, if all else fails, then write this program, and if you like it, try the Project Euler.

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This morning, I wrote: . . .
Challenge: write a program to print the last 10000 digits of 1000000!
Challenge enhanced Take not more than two minutes to do it!
 
Paul Murray-Cbr
Greenhorn
Posts: 12
Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Skye Antinozzi wrote:Is there a way to pop a method of the stack or clear part of the stack so I can keep this going?

This is what methods always do when they finish.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I shall let you out of your misery about the last 10000 digits of 1000000!
But this solution will vanish automatically tomorrow
<vanished code>


Hey!!! Spoiler!
But of course the proof? In Project Euler you have similar questions,
and BigIntegers make it very easy indeed.

So here's a new challange: on how many zeroes does 1_000_000_000! end?

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too late to work that out but 1000000! ends with 249998 zeros (I think).
 
Skye Antinozzi
Ranch Hand
Posts: 68
3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kinda snuck up on me there. Lol. Thanks for all the feedback, fellas. I will give that challenge a shot when I have time, even though it looks like you've already answered it. I have upcoming exams so I won't be on the Ranch much this weekend or this week. Thanks again. ^_^
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Too late to work that out but 1000000! ends with 249998 zeros (I think).

Correct! Thumb up!

Greetz,
Piet
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!