Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Shrunk array takes more memory than original

by BrowserUk (Patriarch)
on Nov 10, 2015 at 13:06 UTC ( [id://1147355]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Shrunk array takes more memory than original
in thread Shrunk array takes more memory than original

why it gets larger from having half of its elements removed.

Because using $#array causes some magic to be attached to the array.

You don't even have to assign to $#array for this to happen, only reference it:

@b = 1 .. 10; print size \@b;; 256 print $#b; print size \@b;; 9 440

And yes, 184 bytes does seem excessive. You'd think that the magic could be stored once and simply referenced for each array using an 4/8-byte pointer.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Shrunk array takes more memory than original
by dave_the_m (Monsignor) on Nov 10, 2015 at 13:12 UTC
    Because using $#array causes some magic to be attached to the array.
    Technically speaking, a temporary magic object is created that points to the array. When the object is assigned to, it truncates the array. This object is freed (well, returned to the pool anyway) at the end of the statement. The array itself never gains any magic.

    Dave.

      This object is freed (well, returned to the pool anyway) at the end of the statement.

      Thanks for the correction. But it does raise the question of what is in those 184 bytes that remain?


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
      In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1147355]
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-03-29 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found