# Arrays : When does the memory get used ?

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Hi,

Considering  the following :

use strict;
use warnings;

my @array = (100,200);
\$array[50000] = 300;

How much space is actually used by the array? Is it only for 3 values
(\$array[0], [1], [50000]), or for *all* the intervening indices also ?

if(defined \$array[2000])

returns false. Does this mean that the space is, in fact, not allocated
unless explicitly used?

man perldata yields no answer...any where else where I can look ?

Regards
Abhinav

## Re: Arrays : When does the memory get used ?

Abhinav wrote:
> my @array = (100,200);
> \$array[50000] = 300;
>
> How much space is actually used by the array? Is it only for 3 values
> (\$array[0], [1], [50000]), or for *all* the intervening indices also ?
>
> if(defined \$array[2000])
>
> returns false. Does this mean that the space is, in fact, not allocated
> unless explicitly used?

Some space is allocated - the part that corresponds to the array itself
(simply an array of pointers).  But the elements of the array are
scalars and these are not allocated yet.

However defined() can still report false for an element that has been
allcated - to see if storage is allocated use exists().

> man perldata yields no answer...any where else where I can look ?

perlguts and the sources.

## Re: Arrays : When does the memory get used ?

> Hi,
>
> Considering  the following :
>
> use strict;
> use warnings;
>
> my @array = (100,200);
> \$array[50000] = 300;
>
> How much space is actually used by the array? Is it only for 3 values
> (\$array[0], [1], [50000]), or for *all* the intervening indices also ?
>
> if(defined \$array[2000])
>
> returns false. Does this mean that the space is, in fact, not allocated
> unless explicitly used?
>
> man perldata yields no answer...any where else where I can look ?

You could actually run the above code (with a sleep; at the end, and using
several different values larger than 50,000) and ask your OS how much
memory perl was taking at each one.

On my system, it uses 7 bytes per entervening entry.

Xho

--