[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Any correlation between malloc return value and physical location?

begin Kirk Strauser quote from Sun, Oct 12, 2003 at 10:05:25PM -0500
> I'm pretty sure that the answer to this is "no", but:
> Is there any relationship between the address returned by malloc(3) and the
> physical address of the block of RAM?  I'm almost positive that there isn't
> - after all, there's not much chance of a block being swapped out and back
> in to the same location - but I'm curious.

No.  malloc returns "virtual" addresses to userspace programs.  The RAM
you're given could be anywhere in memory, and only the kernel and the
CPU know the mapping between the virtual addresses programs are given
and the physical addresses that go out over the memory bus.  An effect
of this is that two processes may have a pointer with the exact same
value, but they will be pointing at completely different areas of
physical memory, and the processes will not be able to see the other.

Interestingly, the space isn't even really allocated until you write to

> If I'm correct and there is no correlation, then my next question is:
> If a identical programs (same data, different files) 

Kinda tangential, but if you run multiple copies of the same program (==
from the same file on disk), they share memory for the actual code.
That is, launching ten copies of XMMS will not use appreciably more
memory for the actual executable than launching one would.

The other optimisation is that bits of executable code aren't loaded
into memory until the program actually uses that page of code.

> are launched at the
> same time so that each copy in memory is identical, and they all call
> malloc(some_constant), do they all get the same return address from malloc()
> or do they get different answers?

You're not guaranteed this, but a simple program I just wrote that just
allocates memory and prints the value of the pointer gave me the same
address 20 times in a row.  Of course, you're not supposed to worry
about this sort of thing, and just let the kernel and libc handle it for
you...So, I guess that's a "maybe" ;-)

Rob Weir <rweir@ertius.org> | mlspam@ertius.org  |  Do I look like I want a CC?
Words of the day:        Arnett event security Majic CDMA constitution digicash
Hi, VeriSign!                          bob@7e4822c114df191f78820f683d9cc071.com

Attachment: signature.asc
Description: Digital signature

Reply to: