Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Just two words of caution. If you create a long list-like structure in perl, you have to take care how it is deleted when the reference count drops to zero. For example, this code causes a segfault on my machine:

perl -we '$x = 1; $x = [$x] for 0 .. 0 + $ARGV[0]; $x = ();' 50000
(you might have to increase the numeric argument to reproduce this). This is because as the reference-count garbage-collector deletes $x<code>, it has to delete <code>$$x[0] first, for which it has to delete $$x[0][0] first etc, and it does this thing recursively. This would not normally be a problem, but can cause a problem in two cases. Firstly, stack space is used for this recursion, which is limited, so in the case of such a long list, the stack will be full and that's why you get the error. Indeed, if you look at the backtrace, you'll see a long stream of frames like this:
... #78 0x080bd016 in Perl_sv_free () #79 0x080acd3e in Perl_av_undef () #80 0x080bcd9a in Perl_sv_clear () #81 0x080bd016 in Perl_sv_free () #82 0x080bcad5 in Perl_sv_clear () #83 0x080bd016 in Perl_sv_free () #84 0x080acd3e in Perl_av_undef () #85 0x080bcd9a in Perl_sv_clear () #86 0x080bd016 in Perl_sv_free () #87 0x080bcad5 in Perl_sv_clear () #88 0x080bd016 in Perl_sv_free () #89 0x080acd3e in Perl_av_undef () #90 0x080bcd9a in Perl_sv_clear () #91 0x080bd016 in Perl_sv_free () #92 0x080bcad5 in Perl_sv_clear () ...

The other problem is circular references. A doubly-linked list contains circular references, so if you just undef a reference to it, it will contain circular references, the reference count will never drop to zero, and memory will leak. You can usually avoid this by explicitly undefing some refernces inside the data structure when you want to free the list. The DESTROY method can help you in this. For example, this code leaks memory (again, increase the numeric argument to make it leak more):

perl -we 'for (0 .. $ARGV[0]) { my $y = 1; my $x = \$y; $x = [$x] for +0 .. 1000; $y = $$x[0]; }; system "ps u $$"' 1000

Update: The first problem is not that easy to cure, but it is possible. Doing some quick tests, it seems that the garbage-collector in (newer) pythons, the one in Mathematica, and the one in maple are free from this defect; while the simple one in ruby, -- wierdly -- that of bigloo scheme, and (Update) that of guile too have the same limitation. (This of course doesn't mean anything as those are not refcounting gcs, but mark-and-sweep or some similar.) As for the second one, it's specific to reference counting.


In reply to Re: OOP/Linked List Question by ambrus
in thread OOP/Linked List Question by hok_si_la

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (7)
    As of 2014-10-24 07:26 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (130 votes), past polls