Problems? Is your data what you think it is? | |
PerlMonks |
Re: Re: Re: what does that value mean, when you evaluate hash in a scalar context?by dakkar (Hermit) |
on Dec 14, 2002 at 17:47 UTC ( [id://219875]=note: print w/replies, xml ) | Need Help?? |
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 Section
Seekers of Perl Wisdom
|
|