Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

rcLrecoil

macrumors newbie
Original poster
Jan 4, 2011
5
0
Hi guys, new to the forums, osx and programming, having just started at university and have run into some trouble. I'm creating a program in python that basically simulates a bank teller, moderates the length of the que etc.

Upon running for longer than a few virtual days it crashes leaving an error message.

ill paste what i have and the error message below.

bear in mind the code is not complete, but should be able to run none the less.

(ignore the end graph)

http://pastebin.com/fjLuj8qJ < code

http://pastebin.com/E4XzzV7k < error

it can be re produced on any computer by running the code and entering more than 5 or 6 days to run.

does anyone have any idea how i could fix this??

many thanks.
 
It looks to me like too much recursion.

Each method invocation requires a frame which is stored in the memory reserved for stack. If your stack gets large enough to use all available stack memory, then any attempts to use additional stack space will fail with an unpredictable crash, like the one you're seeing.

This kind of crash often occurs with infinite recursion, but given how heavily your program appears to rely heavily on recursion, you may simply be attempting to recurse too much for larger inputs. You might want to try using a simple loop instead.
 
awww, that's a shame, i was enjoying my little recursive fest.

is there anyway to purge the stack every now and then, or allocate more memory or anything like that or is it going to have to be a case of a complete re write?

thanks for the response.
 
The stack holds important information. If it's purged, you lose information. That isn't something a program usually does intentionally.

The standard way of avoiding death by recursion is to refactor it so the recursive structure is explicit. That is, implement your own stack. This usually takes less memory than relying on the call stack, but it will still have a recursion limit. It also gives you the control necessary to virtualize it to disk if necessary.

The other tool to apply is converting tail-recursion to iteration. Search Wikipedia for keywords tail recursion.
 
thanks guys

hi guys, thanks very much for your input, i could not work out how to do that special recursion to save the stack so i re wrote the code in while loops instead and now it's fine.

thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.