Explanation for observed behaviour:

  1. Perl arrays take the form of a C pointer to a pointer array.
  2. Each element in the pointer array refers to a single scalar value, or the value NULL.
  3. Arrays are automatically extended during assignment when they are indexed with an index that does not exist.
  4. Array extension initializes the newly created elements to the value NULL.
  5. From Perl, array elements with the value NULL appear to be the undef() value.
  6. The undef() value does not have any qualifiers, and is reused. All simple undef() values referenced from Perl can refer to the same scalar data structure internally, minimizing memory usage.
  7. The special scalar data structure used to indicate the undef() value is marked as an 'immortal scalar'.
  8. Immortal scalars are given artificially large reference counts to minimize the code path that normally decrements the reference count, and if the reference count reaches zero, deallocates the scalar. Immortals live even if the reference count reaches zero, so by setting the reference count to a very large value each time it reaches zero, the reference decrement code is simplified on the critical path.

Summary:

Perl supports sparse arrays of a sort. Arrays that are extended, but not explicitly initialized do not require nearly as much memory as arrays that are extended and explicitly initialized. On an architecture with 32-bit C pointers, an array of length 1000000, that has not been explicitly initialized, will require only 4000000 bytes of RAM.


In reply to Re: Sparse arrays? by MarkM
in thread Sparse arrays? by diotalevi

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.