If you're coming from a C++ background, you can think of Perl arrays as having most of the characteristics of a std::vector<SV*>, and a few of the characteristics of a std::deque<SV*>, where an SV* is a pointer to a scalar value, which may contain anything that scalars are allowed to contain.

In specific, Perl arrays provide constant time index lookup, linear time mid-array insertions or deletions, amortized constant time insertion or deletion at the beginning or end of the container, and constant time determination of size.

How that is implemented internally should be less important than the computational complexity of the operations you can perform on an array. In C++, vectors are guaranteed to have contiguous memory, and deque containers are not guaranteed to inhabit contiguous memory. In C++, this implementation detail leaks out of the internals into the language abstractions, in that you can copy a block of memory from an old fashioned C array to a vector of adequate size, and have an expectation that it will work. C++ also lets you do pointer arithmetic with vectors, but not with deques. But forward, reverse, and random access iterators exist for both classes to minimize the need for pointer arithmetic. Most of the time a C++ programmer is going to be satisfied just knowing the computational complexity of the various operations that one can perform on a given container class.

Internally Perl's arrays do inhabit contiguous memory, but unlike C++, that implementation detail never leaks outside of the guts layter, into the abstraction of the Perl language itself. You would have to dive into XS programming to care, and even then, you mostly don't need to worry about it.

As for memory usage, take whatever amount of memory you expect to be using, double it, and then multiply that by ten, and you won't underestimate by too much.(Attribution needed) ;) Perl's array elements consist of pointers to scalars, which internally are structures with many sub-components.


Dave


In reply to Re: how array variables are stored in memory by davido
in thread how array variables are stored in memory by vipsnoida

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.