This is a weird one, or at least it seems that way to me.

Granted though, I'm feeling like I'm missing something very obvious. So I may just be dumb, here.

Here's the scenario: I have the following code. It looks fairly straightforward: I create a new, empty list, then try to assign to element 10. That works fine. Then I dump out the array with Data::Dumper.

Then I create a new variable, assign a dummy string to it, and try to dump out my original array, again.

And I get a core dump, along with a bunch of "Attempt to free unreferenced scalar." warnings. How many? 10. Which is how many undefined elements are in my array, before the 11th element, which I defined.

Now to be honest, I *think* I can see what's going on: I create a new array, then fill in the 11th element, but leave the first 10 elements undefined. What seems to be happening is that Perl is getting rid of elements 0-9 (dropping their reference count to 0, then reclaiming the memory. Then Perl tries to write the data for the new variable ($base) over the memory originally held by my (still in scope) array.

So, the question: can anyone reproduce my results, which I've posted below? I'm running Perl5.005_03:

Summary of my perl5 (5.0 patchlevel 5 subversion 3) configuration: Platform: osname=linux, osvers=2.2.5-22smp, archname=i386-linux
So it's possible that this is a weird bug on my machine, and also possible that it's a bug in my version of Perl, fixed in 5.6. Who knows. Can anyone reproduce it, and if so, is it fixed in 5.6? (I don't have 5.6). Here's the code:
#!/usr/bin/perl -w use strict; use Data::Dumper; my @comps; $comps[10] = "foo bar"; print Dumper \@comps; my $base = "Foo bar"; print Dumper \@comps;
And the results:
$VAR1 = [ undef, undef, undef, undef, undef, undef, undef, undef, undef, undef, 'foo bar' ]; Bizarre copy of ARRAY in aassign at /usr/lib/perl5/5.00503/Data/Dumper +.pm line 207. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Attempt to free unreferenced scalar. Segmentation fault (core dumped)
Thanks.

In reply to Core Dumping with Arrays by btrott

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.