You are right, given the appropriate frame of reference ;-)))

Old OSs actually gave you the memory you requested.

In modern, sane operating systems, allocating memory is not the same as occupying it. If you do char* m=malloc(10*1204*1024), you are telling the OS (OK, the C library, which could do some magic, but the result is the same) that you want a megabyte added to your address space. So the OS (for example) adds a couple of lines to your process' page table, and you have your address space. When then you do c[200]=0, the OS tries to access the memory, gets a page fault, remembers that it has to actually give you that memory, and creates a page (a single page, mind, usually in the order of 4KB) to store your data in, attaching it to the appropriate entry in the page table. Now you are occupying memory. 4KB of it ;-)

So the space occupied is less than the space allocated. Given that most of the time is spent in the allocation phase (you have to keep track of the memory at the OS, library and probably application levels), always doubling the memory allocated to a growing container insures an "amortized linear time" complexity (meaning that it's linear in the mean, but sometimes it is slower (alloc), sometimes faster (write)).

-- 
        dakkar - Mobilis in mobile

In reply to Re: Re: Re: what does that value mean, when you evaluate hash in a scalar context? by dakkar
in thread what does that value mean, when you evaluate hash in a scalar context? by pg

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.