Arrays : When does the memory get used ?

A

Abhinav

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
 
B

Brian McCauley

Abhinav said:
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.
 
C

ctcgag

Abhinav said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,160
Messages
2,570,889
Members
47,422
Latest member
LatashiaZc

Latest Threads

Top