<?xml version="1.0" encoding="windows-1252"?>
<node id="1001375" title="Re: Wrong dereference =&gt; Out of memory" created="2012-10-29 11:10:09" updated="2012-10-29 11:10:09">
<type id="11">
note</type>
<author id="961">
Anonymous Monk</author>
<data>
<field name="doctext">
&lt;p&gt;First of all, this is about &lt;a href="http://perldoc.perl.org/5.8.9/perlref.html#Pseudo-hashes:-Using-an-array-as-a-hash"&gt;&lt;b&gt;pseudo-hashes&lt;/b&gt;&lt;/a&gt;. This (now long gone) feature lets you access an array reference as if it was a hash reference.&lt;/p&gt;
&lt;p&gt;So what does &lt;code&gt;$aref-&gt;{key}&lt;/code&gt; actually do? Well, this: &lt;code&gt;$aref-&gt;[$aref-&gt;[0]{key}]&lt;/code&gt;. That is, it fetches the first element of the array (which must be a hashref) and uses it to map the key to an array index, which is then finally looked up in &lt;code&gt;@$aref&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The other piece of the puzzle is &lt;b&gt;autovivification&lt;/b&gt; (also explained in perldoc perlref). This is what automatically creates references for you when you try to dereference something that doesn't exist. Example: &lt;code&gt;
my $foo; $foo-&gt;[1] = 42;  # automatically sets $foo = [] before the
# assignment so you end up with $foo = [undef, 42] instead of
# dying with "Can't use undef as an ARRAY reference"&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;OK, now let's look at your code again. &lt;code&gt;$a-&gt;{test}&lt;/code&gt; looks up "test" in &lt;code&gt;{ test =&gt; { val =&gt; 1 }}&lt;/code&gt; (the first element of &lt;code&gt;@$a&lt;/code&gt;) and returns &lt;code&gt;{ val =&gt; 1 }&lt;/code&gt;. It then uses this reference as an array index, which converts it to a (very large) integer (the internal memory address of the hash).&lt;/p&gt;
&lt;p&gt;&lt;code&gt;$a-&gt;[0xDEADBEEF]&lt;/code&gt; obviously doesn't exist; the array only has two elements. But since you're dereferencing it (the &lt;code&gt;-&gt;{val}&lt;/code&gt; part), perl tries to autovivify a hashref for you. Unfortunately this involves resizing the array to have 0xDEADBEEF + 1 elements (the intervening elements are set to &lt;code&gt;undef&lt;/code&gt;). At this point perl is running out of memory, as expected.&lt;/p&gt;</field>
<field name="root_node">
1001362</field>
<field name="parent_node">
1001362</field>
<field name="reputation">
32</field>
</data>
</node>
