Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Odd: Out of memory! error

by BlueLines (Hermit)
on Feb 23, 2002 at 01:16 UTC ( [id://147010]=perlquestion: print w/replies, xml ) Need Help??

BlueLines has asked for the wisdom of the Perl Monks concerning the following question:

This came from a coworker. Consider this:
#!/usr/bin/perl -w my $foo = { c => {} }; my $records = [ $foo ]; $records->{c} = 1;
What happens when this is run?
[jon@opiate jon]$ ./foo.pl Out of memory! [jon@opiate jon]$ perl --version This is perl, v5.6.0 built for i386-linux
Why is this?

BlueLines

Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Edit kudra 2002-02-23 Added to title

Replies are listed 'Best First'.
Re: Odd
by wog (Curate) on Feb 23, 2002 at 01:51 UTC

    Your code uses something called pseudo-hashes. Quoth perlref (bleedperl):

    [stuff about this being an experimental feature, and the user-visible implementation of pseudohashes begin deprecated in perl 5.8 and slated to go away in perl 5.10.]

    Beginning with release 5.005 of Perl, you may use an array reference in some contexts that would normally require a hash reference. This allows you to access array elements using symbolic names, as if they were fields in a struc ture.

    For this to work, the array must contain extra informa tion. The first element of the array has to be a hash reference that maps field names to array indices. Here is an example:

    $struct = [{foo => 1, bar => 2}, "FOO", "BAR"];
    [more code snipped]

    You've declared a pseudohash with one element, "c", which is located at the location that {} turns into when it is used as a number. {} is a reference, and when used as a number turns into it's memory location (so it can be compared, mainly). It's memory location is likely to be a very high number. When you access $record->{c}, perl tries to access the array element in @$record at the index specified by that memory location. Since that array element doesn't exist perl tries to expand the array, and fails saying it doesn't have enough memory. (Since arrays can't be stored with holes in perl.)

Re: Odd
by Zaxo (Archbishop) on Feb 23, 2002 at 01:47 UTC

    $records is an array reference, you dereference it as a hash. The result is that you try to assign 1 to a very large index, autovivifying everything below that. I get the message:

    Out of memory during "large" request for 1073745920 bytes, total sbrk( +) is 1616744 bytes at oop.pl line 6 (#1)

    Update: This is right as far as it goes, but ++wog nailed this question and settled BlueLines's objection at the same time.

    After Compline,
    Zaxo

      Nope. Perl throws an exception when you try to dereference an array ref as a hash. Try it:
      [jon@devotchka jon]$ perl my $a = []; $a->{z} = 1; Can't coerce array into hash at - line 2.


      BlueLines

      Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://147010]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-19 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found