[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] That was productive...



On Tue, Jan 18, 2000 at 06:07:56PM -0500, John Porter wrote:
> Ronald J Kimball wrote:
> > On Tue, Jan 18, 2000 at 12:35:25PM -0500, John Porter wrote:
> > > * Jeff Pinyan (jeffp@crusoe.net) [000118 11:54]:
> > > >
> > > > Sometimes, I use something like:
> > > >
> > > >   for ($i = 0; local $_ = $array[$i], $i < @array; $i++) {
> > > >     ...
> > > >   }
> > >
> > > I'm not sure, but I think that may trigger the array
> > > extension mechanism, since sometimes you're accessing
> > > $array[ $#array + 1 ].
> > 
> > Taking a reference to an unallocated element of an array will extend the
> > array.  
> > 
> > Simply using an unallocated element as an rvalue will not affect the
> > contents of the array, however.
> > 
> > print scalar(@array), "\n";
> 
> Um, I'm talking about the internally allocated space for
> the array, not the publicly reported size.
> You know, the mechanism that allows arrays to be dynamically
> sized in perl.
> 

That would be a rather serious design error, wouldn't you say?  There's no
need to allocate space for an array if the space isn't being used for
anything.

Anyway, as you can see, you have nothing to worry about:

#!perl -w

use strict;

my @a;
my $i;

for $i (4..10) {
    $_ = $a[1 << ($i * 2)];
    printf "%2d %s", $i, `ps -o comm= -o vsz= | grep perl`;
}

print "\n";

for $i (4..10) {
    $a[1 << ($i * 2)] = undef;
    printf "%2d %s", $i, `ps -o comm= -o vsz= | grep perl`;
}

__END__

 4 perl        2732 
 5 perl        2732 
 6 perl        2732 
 7 perl        2732 
 8 perl        2732 
 9 perl        2732 
10 perl        2732 

 4 perl        2732 
 5 perl        2732 
 6 perl        2768 
 7 perl        2800 
 8 perl        3248 
 9 perl        4784 
10 perl       10928 


Perl's memory usage increases only when elements past the end of the array
are actually assigned to, not when they are merely accessed.

Ronald

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe