Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: memory leak

by Athanasius (Prior)
on Jul 27, 2013 at 06:52 UTC ( #1046632=note: print w/ replies, xml ) Need Help??


in reply to memory leak

Hello ch.sarath, and welcome to the Monastery!

It looks as though you are trying to compute a variation of the Ackermann function (note the spelling), which is notable precisely because “Its value grows rapidly, even for small inputs.” Of course you are running out of memory (that’s kind of the point!), but this has nothing to do with a memory leak, and there is no way to “eliminate” this problem when designing recursive functions. Update: Seems I was too hasty here: consider Memoization as a technique for managing these kinds of problems. Thanks to Anonymous Monk, below. Best reference on memoization I know: Chapter 3 of Higher-Order Perl by Mark Jason Dominus.

Some additional notes:

  • You should use warnings.
  • I don’t think undef $i and undef $j are needed at all, but in any case they should be $i = 0 and $j = 0 respectively.
  • Don’t call functions with an initial & unless you have a good reason to turn off prototyping. Just use: my $ackermann = ackermann($i, $j).

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


Comment on Re: memory leak
Select or Download Code
Re^2: memory leak
by Anonymous Monk on Jul 27, 2013 at 07:00 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1046632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2014-04-20 19:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (487 votes), past polls